The eighth JAVA homework "combination of objects"

CG system URL: http://211.81.175.89

【Problem Description】

1. Define the TableInfo class, which has three attributes: shape (String type), height (int type), and owner (Worker type). Add the necessary construction methods and setter methods to the TableInfo class, and define the print method to output "This is zhangsan's table"

2. Define the Worker class, which has two attributes: name (String type) and table table (TableInfo type). Add the necessary construction methods and setter methods to the Worker class, and define the print method to output "zhangsan has a square table"

3. Define the test class Test, the code of the main method is as follows:

【Input form】

Two strings, one is the shape of the table, and the other is the name of the worker
[output format]

Two sentences:

This is zhangsan's table

zhangsan has a square table

【Sample input】

please input table's shape:

square

please input worker's name:

zhangsan

【Sample output】

This is zhangsan's table

-----

zhangsan has a square table

【Example description】

where the shape of the table and the name of the worker are input variables

The program is difficult to understand the statement:

this.table.setOwner(this);

1. The statement is located in the constructor of Worker

2. So this is the current Worker object

3. Call the method setOwner of the attribute table object of the current object, and the parameter this is still the current object

4. It means that the owner of the table of this Worker object is this Worker

 

import java.util.Scanner;

class TableInfo{
    private String shape;
    private int height;
    private Worker owner;

    public TableInfo(String shape, int height) {
        this.shape = shape;
        this.height = height;
    }
    //用置取方法设置table对象的owner
    public void setOwner(Worker owner) {
        this.owner = owner;
    }
    //返回shape属性
    public String display(){
        return shape;
    }
    //输出
    public void print(){
        System.out.println("This is "+ owner.display() +"'s table");
    }
}

class Worker{
    private String name;
    private TableInfo table;
    //输出
    public void print(){
        System.out.println(name+" has a "+table.display()+" table");
    }
    //返回name属性
    public String display(){
        return name;
    }
    //构造方法
    public Worker(String name, TableInfo table) {
        this.name = name;
        this.table = table;
        this.table.setOwner(this);
    }
}
public class two {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in) ;
        System.out.println("please input table's shape:");
        String shape = sc.next();
        TableInfo table= new TableInfo(shape,80) ;
        System.out.println("please input worker's name:");
        String name = sc.next();
        Worker worker = new Worker(name ,table) ;
        table.print();
        System.out.println("-----") ;
        worker.print();

    }
}

 

Guess you like

Origin blog.csdn.net/qq_25887493/article/details/123847794