JAVA构造方法的题目(初学)
【题目1】设计一个Dog类,有名字、颜色和年龄属性,定义构造方法初始化这些属性,定义输出方法show()显示其信息。编写应用程序使用Dog类publicclassDog{...
【题目1】设计一个Dog类,有名字、颜色和年龄属性,定义构造方法初始化这些属性,定义输出方法show()显示其信息。编写应用程序使用Dog类
public class Dog {
/*…源代码…*/
public static void main(String[] args) {
Dog ram=new Dog("Ram","white",2);
ram.show();
/*…源代码…*/
}
}
运行后程序输出:
name:Ram,color=white,age=2
name:nim,color=black,age=3 展开
public class Dog {
/*…源代码…*/
public static void main(String[] args) {
Dog ram=new Dog("Ram","white",2);
ram.show();
/*…源代码…*/
}
}
运行后程序输出:
name:Ram,color=white,age=2
name:nim,color=black,age=3 展开
展开全部
public class Dog {
private String Name;
private String Color;
private int Age;
public Dog() {
}
public Dog(String name, String color, int age) {
this.setName(name);
this.setColor(color);
this.setAge(age);
}
public static void main(String[] args) {
Dog ram=new Dog("Ram","white",2);
ram.show();
Dog Nim=new Dog("nim","black",3);
Nim.show();
}
private void show() {
System.out.println("name:"+this.getName()+",color="+this.getColor()+",age="+this.getAge());
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getColor() {
return Color;
}
public void setColor(String color) {
Color = color;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
}
private String Name;
private String Color;
private int Age;
public Dog() {
}
public Dog(String name, String color, int age) {
this.setName(name);
this.setColor(color);
this.setAge(age);
}
public static void main(String[] args) {
Dog ram=new Dog("Ram","white",2);
ram.show();
Dog Nim=new Dog("nim","black",3);
Nim.show();
}
private void show() {
System.out.println("name:"+this.getName()+",color="+this.getColor()+",age="+this.getAge());
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getColor() {
return Color;
}
public void setColor(String color) {
Color = color;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
}
追问
谢谢!想问下this是什么方法吗?
追答
这里this代指Dog 类,就是当前的类
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |