4.23 第八周上机作业

定义一个笔记本类,该类有颜色(char)和cpu
型号(int)两个属性。 [必做题]
• 3.2 输出笔记本信息的方法
• 3.3 然后编写一个测试类,测试笔记本类的各个方法

package test2;

public class Notebook {
    char color;
    int cpu;
    
    public void show(){
        System.out.println("该笔记本的颜色是  " + color);
        System.out.println("该笔记本的cpu型号  " + cpu);
    }
    
}
package test2;

public class book {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Notebook a1 = new Notebook();
        a1.color = '白';
        a1.cpu = 1112;
        a1.show();
        
    }

}

一、定义两个类,描述如下: [必做题]
• 5.1定义一个人类Person:
• 5.1.1定义一个方法sayHello(),可以向对方发出问
候语“hello,my name is XXX”
• 5.1.2有三个属性:名字、身高、体重
• 5.2定义一个PersonCreate类:
• 5.2.1创建两个对象,分别是zhangsan,33岁,1.73
;lishi,44,1.74
• 5.2.2分别调用对象的sayHello()方法。

package test1;

public class Person {
    String name;
    double height;
    double weight;
    int age;
    
    public void sayHello(){
        System.out.println("hello,my name is " + name );
        System.out.println("my height is " + height);
        System.out.println("my age is " + age);
    }
    
    
    
}
package test1;

public class PersonCreate {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person p1 = new Person();
        Person p2 = new Person();     
        p1.name="zhangsan";
        p1.height=1.73;
        p1.age=33;
         
        p2.name="lishi";
        p2.height=1.74;
        p2.age=44;
         
        p1.sayHello();
        p2.sayHello();
    }

}

posted @ 2020-04-23 16:07  LSiyN  阅读(106)  评论(0编辑  收藏  举报