第八周上机作业
3.定义一个笔记本类,该类有颜色(char)和cpu
型号(int)两个属性。 [必做题]
• 3.1 无参和有参的两个构造方法;有参构造方法可
以在创建对象的同时为每个属性赋值;
• 3.2 输出笔记本信息的方法
• 3.3 然后编写一个测试类,测试笔记本类的各个
方法。
package yes;
public class hello {
char color;
int cpu;
public hello() {
System.out.println("无参数");
}
public hello(char a,int b) {
color=a;
cpu=b;
System.out.println(color);
System.out.println(cpu);
System.out.println("有参数");
}
public void xinxi(){
System.out.println("颜色是"+color);
System.out.println("cpu");
}
}
package xx;
public class pika {
public static void main(String[] args) {
// TODO Auto-generated method stub
hello b1=new hello();
hello b2=new hello('白',i5);
b2.xinxi();
}
}
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()方法。
package chap3;
public class Person {
public String name;
double sg;//属性
int tz;//属性
int age;
public Person() {
}
public Person(String name ,double sg,int tz,int age) {
this.name=name;
this.sg=sg;
this.tz=tz;
this.age=age;
}
public void sayHello() {
//成员方法
System.out.println("hello my name is " + this.name);
}
}
package chap3;
public class abc {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person i1=new Person();
i1.name="崔笑荣";//成员变量
i1.sg=1.99;//成员变量
i1.tz=380;//成员变量
Person i2=new Person();
i2.name="zhangsan";
i2.sg=1.73;
i2.tz=300;
i2.age=33;
Person i3=new Person();
i3.name="lishi";
i3.sg=1.74;
i3.tz=500;
i3.age=44;
i1.sayHello();
i2.sayHello();
i3.sayHello();
}
}
浙公网安备 33010602011771号