第八周上机练习
1、定义一个笔记本类,该类有颜色(char)和cpu
型号(int)两个属性。 [必做题]
• 3.1 无参和有参的两个构造方法;有参构造方法可
以在创建对象的同时为每个属性赋值;
• 3.2 输出笔记本信息的方法
• 3.3 然后编写一个测试类,测试笔记本类的各个
方法。
package project1; public class a1 { char color; int model; a1() { } a1(char color, int model) { this.color = color; this.model = model; } void a() { System.out.println("颜色:" + color + " 型号:" + model); } public static void main(String[] args) { a1 a = new a1('白', 9527); a.a(); } }
2
package project1; public class a1 { public String name; double sg; int tz; int age; public a1() { } public a1(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 project1; public class Constructor { public static void main(String[] args) { // TODO Auto-generated method stub a1 i1=new a1(); i1.name="zhangsan"; i1.sg=1.73; i1.tz=300; i1.age=33; a1 i2=new a1(); i2.name="lishi"; i2.sg=1.74; i2.tz=500; i2.age=44; i1.sayHello(); } }