第八周上机作业
3、定义一个笔记本类,该类有颜色(char)和cpu
型号(int)两个属性。 [必做题]
• 3.1 无参和有参的两个构造方法;有参构造方法可
以在创建对象的同时为每个属性赋值;
• 3.2 输出笔记本信息的方法
• 3.3 然后编写一个测试类,测试笔记本类的各个
方法。
5、定义两个类,描述如下: [必做题]
• 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()方法。
1.package itheima01.com;
public class 笔记本3 {
char color;
int cpu;
笔记本3(char string,int i){
color=string;
cpu=i;
void show 笔记本3(string,i);
System.out.println("该笔记本的颜色是"+color+ "该笔记本的cpu名称是"+cpu);
}
笔记本3(){
}
void show笔记本3(char string,int i){
color=string;
cpu=i;
System.out.println("该笔记本的颜色是"+color+" 该笔记本的cpu名称是"+cpu);
}
}
package itheima01.com;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
笔记本3 d=new 笔记本3();
d.show笔记本3('b',007);
}
}
2.package itheima01.com;
public class Person {
String name;
double height;
int age;
Person(String name,double height,int age){
this.name=name;
this.height=height;
this.age=age;
}
void sayHello(){
System.out.println("Hello,my name is "+name+
" My weight "+height+"kg"+" I am "+age+" years old");
}
}
package itheima01.com;
public class PersonCreate {
public static void main(String[]args){
Person i=new Person("zhangsan",1.73,33);
Person j=new Person("lishi",1.74,44);
i.sayHello();
j.sayHello();
}
}


浙公网安备 33010602011771号