Standard class code writing and testing (construction method VS member method)

Student code writing and testing

package Object;
/*
标准的学生类代码的编写和测试
 */
public class StandardKindtest {
    
    
    private String name;
    private int age;
    //构造方法
    public StandardKindtest() {
    
    }
    public StandardKindtest(String name,int age){
    
    
        this.name=name;
        this.age=age;
    }
    //成员方法
    public void setName(String name) {
    
    
        this.name = name;
    }
    public String getName(){
    
    
        return name;
    }
    public void setAge(int age) {
    
    
        this.age = age;
    }
    public int getAge(){
    
    
        return age;
    }
}

//Test class

package Object;

public class StandardKindtestDemo {
    
    
    public static void main(String[] args) {
    
    
        //带参构造方法的调用
        StandardKindtest s=new StandardKindtest("Tony",20);
        System.out.println("姓名:"+s.getName()+" 年龄:"+s.getAge());

        //无参成员方法的调用
        StandardKindtest s1=new StandardKindtest();
        s1.setName("Mary");
        s1.setAge(22);
        System.out.println("姓名:"+s1.getName()+" 年龄:"+s1.getAge());
    }
}

Code writing and testing for mobile phones

package Object;
//手机类的代码编写和测试
public class PhoneKindTest {
    
    
    //品牌、颜色、价格
    private String brand;
    private String color;
    private int price;
    //构造方法
    public PhoneKindTest(){
    
    }
    public PhoneKindTest(String brand,String color,int price){
    
    
        this.brand=brand;
        this.color=color;
        this.price=price;
    }

    //成员方法

    public void setBrand(String brand) {
    
    
        this.brand = brand;
    }
    public String getBrand(){
    
    
        return brand;
    }

    public void setColor(String color) {
    
    
        this.color = color;
    }
    public String getColor(){
    
    
        return color;
    }

    public void setPrice(int price) {
    
    
        this.price = price;
    }
    public int getPrice(){
    
    
        return price;
    }
}

//Test class

package Object;

public class PhoneKindTestDemo {
    
    
    public static void main(String[] args) {
    
    
        //有参构造方法的调用
        PhoneKindTest s=new PhoneKindTest("大众","银灰色",79999);
        System.out.println("品牌:"+s.getBrand()+"  颜色:"+s.getColor()+"  价格:"+s.getPrice());

        //无参成员方法的调用
        PhoneKindTest s1=new PhoneKindTest();
        s1.setBrand("比亚迪");
        s1.setColor("红色");
        s1.setPrice(199999);
        System.out.println("品牌:"+s1.getBrand()+"  颜色:"+s1.getColor()+"  价格:"+s1.getPrice());
    }
}

Guess you like

Origin blog.csdn.net/m0_52646273/article/details/114031761