Java面向对象编程(中级)

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java面向对象编程(中级)相关的知识,希望对你有一定的参考价值。

面向对象编程(中级)

访问修饰符

封装

01:

public class Encapsulation01
{
    public static void main(String[] args)
    {
        Person person = new Person();
        person.name = "Tom";
        person.setAge(30);
        person.setSalary(30000);
    }
}

class Person
{
    public String name;
    private int age;
    private double salary;
    
    public void setSalary(double salary)
    {
        this.salary = salary;
    }
    
    public double getSalary()
    {
        return this.salary;
    }
    
    public void setAge(int age)
    {
        if (age >= 1 && age <= 123)
        this.age = age;
        else 
        {
            System.out.println("Input Error");
            this.age = 18;//给个默认age
        }
    }
    
    public int getAge()
    {
        return this.age;
    }
    
    
    public void setName(String name)
    {
        if (name.length() >= 2 && name.length() <= 6)
        this.name = name;
        else
        {
            System.out.println("Input Error");
            this.name = "wuming";
        }
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public String printElem()
    {
        return "name = "+name+" age = "+age+" salary = "+salary;
    }
    
}

01:

public class Encapsulation01
{
    public static void main(String[] args)
    {
        Person person = new Person();
        person.name = "Tom";
        person.setAge(30);
        person.setSalary(30000);
    }
}

class Person
{
    public String name;
    private int age;
    private double salary;
    
    public Person(){}
    
    public Person(String name,int age,double salary){
        //this.name = name;
       // this.age = age;
       // this.salary = salary;
        setName(name);
        setAge(age);
        setSalary(salary);
    }
    
    public void setSalary(double salary)
    {
        this.salary = salary;
    }
    
    public double getSalary()
    {
        return this.salary;
    }
    
    public void setAge(int age)
    {
        if (age >= 1 && age <= 123)
        this.age = name;
        else 
        {
            System.out.println("Input Error");
            this.age = 18;//给个默认age
        }
    }
    
    public int getAge()
    {
        return this.age;
    }
    
    
    public void setName(String name)
    {
        if (name.length() >= 2 && name.length() <= 6)
        this.name = name;
        else
        {
            System.out.println("Input Error");
            this.name = "wuming";
        }
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public String printElem()
    {
        return "name = "+name+" age = "+age+" salary = "+salary;
    }
    
}

小练习

package HelloDemo;

public class Account {
    private String name;
    private double balance;
    private String password;


    public Account(){}

    public Account(String name, double balance, String password) {
       this.setBalance(balance);
       this.setName(name);
       this.setPassword(password);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if(name.length() >= 2 && name.length() <= 4)
        this.name = name;
        else
        {
            System.out.println("Input Error");
            this.name = "wuming";
        }
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        if (balance > 20)
        this.balance = balance;
        else {
            System.out.println("Input Error,balance = 0");

        }
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        if (password.length()==6)
        this.password = password;
        else
        {
            System.out.println("Input Error password = 000000");
            this.password = "000000";
        }
    }


    public void showInfo()
    {
        //可以增加权限的校验
        System.out.println("name = "+name+" balance = "+balance+" password = "+password);
    }
}


//-------------------------------------------------------------------------------


package HelloDemo;

public class TestAccount {
    public static void main(String[] args)
    {
        Account account = new Account();
        account.setName("Tom");
        account.setBalance(60);
        account.setPassword("123456");

        account.showInfo();
    }
}

继承

01:

package JiCWorkDemo;

public class Pupil {
   public String name;
   public int age;
   private double score;

    public void setScore(double score) {
        this.score = score;
    }

    public void testing()
    {
        System.out.println("pupil name= "+name);
    }

    public void showInfo()
    {
        System.out.println("name = "+name+" age = "+age+" score = "+score);
    }
}


//===========================================


package JiCWorkDemo;

public class Graduate extends Pupil{
    public void testing()
    {
        System.out.println("Graduate name= "+name);
    }
}


//===========================================


package JiCWorkDemo;

public class WorkDemo {
    public static void main(String[] args)
    {
        Pupil pupil = new Pupil();
        pupil.name = "Tom";
        pupil.age = 11;
        pupil.testing();
        pupil.setScore(50);
        pupil.showInfo();

        System.out.println("========");

        Graduate graduate = new Graduate();

        graduate.name = "Jack";
        graduate.age = 23;
        graduate.testing();
        graduate.setScore(80);
        graduate.showInfo();

    }
}

继承的深入讨论/细节问题

01:

package JiCWorkDemo;

public class Base {
    public int n1 = 100;
    protected  int n2 = 200;
    int n3 = 300;
    private int n4 = 400;

    public int getN4()
    {
        return n4;
    }
    
    
    public void callTest400()
    {
        test400();
    }
    public Base()
    {
        System.out.println("Base()......");
    }

    public void test100()
    {
        System.out.println("test100");
    }

    protected void test200()
    {
        System.out.println("test200");
    }

    void test300()
    {
        System.out.println("test300");
    }

    private void test400()
    {
        System.out.println("test400");
    }

}


//==============================================


package JiCWorkDemo;

public class Son extends Base{
    public Son()
    {
        System.out.println("sub()...");
    }

    public void say0k()
    {
        System.out.println(n1+n2+n3);
        test100();
        test200();
        test300();
        //System.out.println(n4);//Error
        //test400();//Error

        System.out.println("n4 = "+getN4());
        callTest400();
    }
}

继承的本质分析

如果查找过程中,父类的age是private,而爷爷类的age是public,但还是会报错,因为找到父类的age是private以后就会报错,不会去找爷爷类的age

小练习

结果如下:

我是A类

hahah我是B类的有参构造

我是c类的有参构造

我是c类的无参构造

01:

package ExerciseDemoWork;

public class Computer {
    private String cpu;
    private int memory;
    private int disk;

    public Computer(String cpu, int memory, int disk) {
        this.cpu = cpu;
        this.memory = memory;
        this.disk = disk;
    }

    public String getDetails()
    {
        return "cpu = "+cpu+" memory = "+memory+" disk = "+disk;
    }

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public int getMemory() {
        return memory;
    }

    public void setMemory(int memory) {
        this.memory = memory;
    }

    public int getDisk() {
        return disk;
    }

    public void setDisk(int disk) {
        this.disk = disk;
    }


}

//============================================================================


package ExerciseDemoWork;

public class PC extends Computer{
    private String brand;

    public PC(String cpu, int memory, int disk, String brand) {
        super(cpu, memory, disk);
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public void Scala核心编程_第08章 面向对象编程(中级补充)--java动态绑定与静态绑定

Scala核心编程_第08章 面向对象编程(中级补充)--java动态绑定与静态绑定

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

C#中级面向对象编程

2022年最新Java工程师面试题从基础到中级到高级