1.定义一个笔记本类,该类有颜色(char)和cpu
型号(int)两个属性。 [必做题]
• 3.1 无参和有参的两个构造方法;有参构造方法可
以在创建对象的同时为每个属性赋值;
• 3.2 输出笔记本信息的方法
• 3.3 然后编写一个测试类,测试笔记本类的各个
方法。
class text3{ public static void main(String[] args) { NoteBookComputer nbc=new NoteBookComputer('黑',1080); System.out.println("颜色;"+nbc.getColor()+" "+"cpu:"+nbc.getCpu()); NoteBookComputer nbc2=new NoteBookComputer(); nbc2.setColor('红'); nbc2.setCpu(1060); System.out.println("颜色;"+nbc2.getColor()+" "+"cpu:"+nbc2.getCpu()); } } public class NoteBookComputer { private char color; private int cpu; public NoteBookComputer() { } public NoteBookComputer(char color, int cpu) { this.color = color; this.cpu = cpu; } public char getColor() { return color; } public void setColor(char color) { this.color = color; } public int getCpu() { return cpu; } public void setCpu(int cpu) { this.cpu = cpu; } }
2.定义两个类,描述如下: [必做题]
• 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()方法。
class personCreate{ public static void main(String[] args) { person p=new person("zhangsan",1.73,33); p.sayHello(); person p2=new person("lisi",1.74,44); p2.sayHello(); } } public class person { private String Name; private double Tall; private int Age; public void sayHello(){ System.out.println("你好我们名字叫"+Name); } public person() { } public person(String name, double tall, int age) { Name = name; Tall = tall; Age = age; } public String getName() { return Name; } public void setName(String name) { Name = name; } public Double getTall() { return Tall; } public void setTall(Double tall) { Tall = tall; } public int getWeight() { return Age; } public void setWeight(int weight) { this.Age = weight; } }