package xiti5;
class Dog{
private String name;
private int age;
private String color;
public Dog(String name,int age,String color){
this.name=name; //(1)构造方法名称必须与类名称一致
this.age=age; //(2)构造方法处不能有任何的返回值类型(写第一遍的时候,出错了,声明为void,应当注意)
this.color=color; //(3)构造方法中不能使用return
}
public void print(){
System.out.println("狗的名字是:"+name+",年龄:"+age+",颜色是:"+color);
}
}
public class Fourth {
public static void main(String[] args) {
Dog dog=new Dog("小强",2,"黑色");
dog.print();
}
}
《Java实战开发经典》第五章5.4
最新推荐文章于 2022-12-15 15:34:33 发布
5126

被折叠的 条评论
为什么被折叠?



