[英]Write a Dog constructor that has one argument, the name, and calls the super constructor passing it the name and the animal type “dog”. cant pass type
public class Pet
{
private String name;
private String type;
public Pet(String n, String t)
{
name = n;
type = t;
}
public String getType(){
return type;
}
public String getName(){
return name;
}
public void speak()
{
System.out.println("grr!");
}
public static void main(String[] args)
{
Pet p = new Pet("Sammy","hamster");
System.out.println(p.getType());
p.speak();
Dog d = new Dog("Fido");
System.out.println(d.getType());
d.speak();
//Cat c = new Cat("Fluffy");
//System.out.println(c.getType());
//c.speak();
}
}
class Dog extends Pet
{
public Dog(String name){
super(name);
}
public void speak(){
System.out.println("Woof");
}
}
// Add a Cat class
How do I add "type" to this without adding another String to my parameter?如何在不向参数中添加另一个字符串的情况下向其添加“类型”? I've tried other ways that obviously didn't work but I still tried anyway.
我尝试了其他显然不起作用的方法,但我仍然尝试过。 So how do I add another object to my super class from Pet without adding more to my parameter?
那么如何从 Pet 添加另一个对象到我的超类而不向我的参数添加更多?
Constructor with one parameter for the dog class:带有狗类的一个参数的构造函数:
public Dog(String name) {
super(name, "dog");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.