5.1.1定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
5.1.2有三个属性:名字、身高、体重
5.1.3通过构造方法,分别给三个属性赋值
5.2定义一个Constructor类:
5.2.1创建两个对象,分别是分别是zhangsan,33,1.73;lishi,44,1.74
5.2.2分别调用对象的sayHello()方法。
publicclassPerson{
String name;double height;double weight;Person(){}Person(String name,double height,double weight){this.name = name;this.height = height;this.weight = weight;}voidsayHello(){
System.out.println("hello,my name is "+ name);}}
publicclassConstructor{publicstaticvoidmain(String[] args){
Person zhangsan =newPerson("zhangsan",1.73,33);
Person lisi =newPerson("lisi",1.74,44);
zhangsan.sayHello();
lisi.sayHello();}}
1、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(int x0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。public class Point { int x; int y; Point(){ ...