1.定义一个笔记本类,该类有颜色(char)和cpu
型号(int)两个属性。 [必做题]
• 1.1无参和有参的两个构造方法;有参构造方法可
以在创建对象的同时为每个属性赋值;
• 1.2 输出笔记本信息的方法
• 1.3 然后编写一个测试类,测试笔记本类的各个
方法。
char color; int number; public char c(char color) { this.color = color; return color; } public int num(int number) { this.number = number; return number; } public void show() { System.out.println("该笔记本的颜色为:" + color); System.out.println("该笔记本的型号为:" + number); }
package homework; public class TestComputer { public static void main(String[] args) { // TODO Auto-generated method stub computer c = new computer(); c.c('红'); c.num(8865927); c.show(); } }
2.定义两个类,描述如下: [必做题]
• 2.1定义一个人类Person:
• 2.1.1定义一个方法sayHello(),可以向对方发出问
候语“hello,my name is XXX”
• 2.1.2有三个属性:名字、身高、体重
• 2.2定义一个PersonCreate类:
• 2.2.1创建两个对象,分别是zhangsan,33岁,1.73
;lishi,44,1.74
• 2.2.2分别调用对象的sayHello()方法。
String name; double height; double weight; public void sayHello() { System.out.println("hello,my name is " + name); }
package homework; public class TestPerson { public static void main(String[] args) { // TODO Auto-generated method stub Person p = new Person(); p.name = "Jack"; p.sayHello(); } }
String name; double height; double weight; public void sayHello() { System.out.println("hello,my name is " + name); System.out.println("hello,my height is"+ height+"M"); System.out.println("hello, my weight is"+ weight+"kg"); }
package homework; public class PersonCreate { public static void main(String[] args) { // TODO Auto-generated method stub Person p1 = new Person(); p1.name = "张三"; p1.height = 1.73; p1.weight = 33; p1.sayHello(); System.out.println("****************************"); Person p2 = new Person(); p2.name = "李四"; p2.height = 1.74; p2.weight = 44; p2.sayHello(); } }