Java uses classes as member variables

Any type can be used as a member variable of a class.
Like the String class that you first came into contact with, it is a class that refers to the data type.
Using the class type as a member variable isIt is equivalent to creating an object from the classPass the address value of the object to the member variable
将成员变量当做对象名进行操作

One, the class as a member variable

Case study
1. Define a Computer class
2. Define two member variables in the Person class, one of which is defined by Computer
3. Write a test class to test the code function

//定义Computer类
class Computer{
    
    
    private String CPU;
    private String RAM;
	//无参有参构造方法,getter和setter方法
    public Computer() {
    
    }
    public Computer(String CPU, String RAM) {
    
    this.CPU = CPU;this.RAM = RAM;}
    public String getCPU() {
    
    return CPU;}
    public void setCPU(String CPU) {
    
    this.CPU = CPU;}
    public String getRAM() {
    
    return RAM;}
    public void setRAM(String RAM) {
    
    this.RAM = RAM;}
}

//定义Person类
class Person{
    
    
    private String name;
    //类作为成员变量
    private Computer computer;
    //无参有参构造方法,getter和setter方法
    public Person() {
    
    }
    public Person(String name, Computer computer) {
    
    this.name = name;this.computer = computer;}
    public String getName() {
    
    return name;}
    public void setName(String name) {
    
    this.name = name;}
    public Computer getComputer() {
    
    return computer;}
    public void setComputer(Computer computer) {
    
    this.computer = computer;}

    //定义一个成员方法,用于打印信息
    public void useComputer(){
    
    
        System.out.println(name+"正在使用电脑的配置是,\nCPU:"+computer.getCPU()+"\n内存:"+computer.getRAM());
    }
}

public class Test {
    
    
    public static void main(String[] args) {
    
    
        //通过构造方法,向Computer的变量赋值
        Computer computer = new Computer("i9-9900k","32G");
        //通过构造方法,向Person的变量赋值
        Person person = new Person("张三丰",computer);
        //用useComputer方法,查看数据传递结果
        person.useComputer();
    }
}

operation result
Insert picture description here

Two, the interface as a member variable

Case study
1. Define an interface Use to be used as a member variable of Person
2. Interface Use must have an implementation class before it can be used
3. Define the interface Use as a member variable
in People 4. Create a member method in the People class and use the interface's Member variables, call abstract methods in the interface.
5. Create a test class to test the code function

interface Use{
    
    
    void use();
}
//创建接口的实现类
class UseComputerImpl implements Use{
    
    
    @Override
    public void use() {
    
    System.out.println("正在使用电脑");}
}

//定义一个People类,将接口作为成员变量
class People{
    
    
    private String name;
    //接口类型作为成员变量
    private Use use;
	//无参有参构造方法,因为没有使用到getter和setter方法,这里省略。平时使用中,不推荐省略
    public People() {
    
    }
    public People(String name, Use use) {
    
    this.name = name;this.use = use;}

    //创建一个使用接口的成员方法,用于打印信息
    public void useComputer(){
    
    
        System.out.print(name);
        //使用接口对象,调用实现类方法
        use.use();
    }
}

//测试类,对编写的代码进行测试
public class Test {
    
    
    public static void main(String[] args) {
    
    
        //创建实现类对象(自动向上转型为接口类型)
        Use use = new UseComputerImpl();
        //使用People的有参构造,向成员变量传递数据
        People people = new People("张三丰",use);
        //使用People的对象调用useComputer方法查看数据传递结果
        people.useComputer();
    }
}

operation result
Insert picture description here
接下来我们扩展一下

useInner class and anonymous object inner class to pass parameters
this is okayOmit the writing of implementation classes
If you need to supplement the relevant knowledge of the internal class, please click here

Create a new Test2 test class, the above code remains unchanged

public class Test2 {
    
    
    public static void main(String[] args) {
    
    

        //使用匿名内部类创建接口对象
        Use use = new Use() {
    
    
            @Override
            public void use() {
    
    
                System.out.println("正在使用电脑");
            }
        };
        //使用People的有参构造,向成员变量传递数据
        People peopleOne = new People("张三",use);
        //使用People的对象调用useComputer方法查看数据传递结果
        peopleOne.useComputer();

//=====================================================
        
        //使用匿名内部类、匿名对象传参
        People peopleTwo = new People("李四", new Use() {
    
    
            @Override
            public void use() {
    
    
                System.out.println("正在使用电脑");
            }
        });
        //使用People的对象调用useComputer方法查看数据传递结果
        peopleTwo.useComputer();
    }
}

operation result
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44580492/article/details/106774923