解释如下代码:public class Dog { String name; Dog(String name) { this.name = name; } String getName() { return this.name; } void setName(String name) { this.name = name; } String getObjectAddress() { return super.toString(); } }
时间: 2023-02-27 21:37:30 浏览: 349
这段代码定义了一个名为Dog的Java类。该类有一个字符串类型的变量name。
该类还有一个带有一个字符串类型参数的构造函数,用于初始化类的name变量。
类中还有两个方法:getName()方法返回name变量的值,setName(String name)方法用于设置name变量的值。
最后一个方法getObjectAddress()返回该对象的字符串表示,它通过调用父类的toString()方法实现。
总的来说,这个Dog类是一个简单的Java类,它模拟了一个狗的对象,拥有名字(name)和获取名字(getName)、设置名字(setName)和获取对象地址(getObjectAddress)的功能。
相关问题
续写代码public abstract class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void makeSound(); } // 包 animal package animal; public class Cat extends Animal { private int age; public Cat(String name, int age) { super(name); this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public void makeSound() { System.out.println("Meow"); } } public class Dog extends Animal { private String breed; public Dog(String name, String breed) { super(name); this.breed = breed; } public String getBreed() { return breed; } public void setBreed(String breed) { this.breed = breed; } @Override public void makeSound() { System.out.println("Woof"); } } public class Elephant extends Animal { private int weight; public Elephant(String name, int weight) { super(name); this.weight = weight; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } @Override要求0.必须有抽象父类,且被继承 1.必须包含 >=2 个包2.必须创建 >=5 个类(其中包含抽象父类)3.类中属性皆为私有属性,需定义读写方法 4.子类需要重写个别父类方法
续写代码:
// 包 animal
package animal;
public abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void makeSound();
}
public class Cat extends Animal {
private int age;
public Cat(String name, int age) {
super(name);
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void makeSound() {
System.out.println("Meow");
}
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
super(name);
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
@Override
public void makeSound() {
System.out.println("Woof");
}
}
public class Elephant extends Animal {
private int weight;
public Elephant(String name, int weight) {
super(name);
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
***
续写这段代码package animal; public abstract class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void makeSound();
public class Cat extends Animal {
private int age;
public Cat(String name, int age) {
super(name);
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void makeSound() {
System.out.println("Meow");
}
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
super(name);
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
@Override
public void makeSound() {
System.out.println("Woof");
}
}
public class Elephant extends Animal {
private int weight;
public Elephant(String name, int weight) {
super(name);
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
***
阅读全文