1.package finish; import java.util.Scanner; public class finish{ public static void main(String[] args){ Computer c1 = new Computer(); c1.showComputer(); Computer c2 = new Computer('红',32); c2.showComputer(); } } class Computer{ private char color; private int cpu; public Computer(){ } public Computer(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; } public void showComputer(){ System.out.println("笔记本的颜色:"+getColor()); System.out.println("笔记本的cpu型号:"+getCpu()); } }
2.package finish; import java.util.Scanner; public class finish{ String name;// 姓名 double height;// 身高 int age;// 年龄 finish(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+" "+height+"m"+" "+age+"岁"); } }
public class personCreate { public void main(String[] args) { person c1 = new person("zhangsan", 1.73, 33); person c2 = new person("lishi", 1.74, 44); c1.sayHello(); c2.sayHello(); } } }