Java 如何让驱动程序类继承超类的某些属性?
我试图让我的驱动程序类继承来自两个不同类的信息。我必须使用公式className objectName=new className(输入参数)来实例化其中一个类。但我一直得到符号未被识别的错误 我不知道如何解决这个问题。我尝试创建一个import语句,但是其他两个类是同一个包的一部分。我也尝试过使用extends关键字,但也尝试过noluckJava 如何让驱动程序类继承超类的某些属性?,java,Java,我试图让我的驱动程序类继承来自两个不同类的信息。我必须使用公式className objectName=new className(输入参数)来实例化其中一个类。但我一直得到符号未被识别的错误 我不知道如何解决这个问题。我尝试创建一个import语句,但是其他两个类是同一个包的一部分。我也尝试过使用extends关键字,但也尝试过noluck public class Corgi extends Dog { // additional class variables priva
public class Corgi extends Dog {
// additional class variables
private static int weight;
private static int age;
// constructor
public Corgi(String type, String breed, String name, int pounds, int years) {
// invoke Dog class (super class) constructor
super(type, breed, name);
weight = pounds;
age = years;
}
// mutator methods
public static int setWeight(int pounds){
weight = pounds;
return pounds;
}
public static int setAge(int years){
age = years;
return years;
}
// override toString() method to include additional dog information
@Override
public String toString() {
return (super.toString() + "\nThe Corgi is " + age +
" years old and weighs " + weight + " pounds.");
}
}
我希望能够让主类通过Corgi tricker=new Corgi()继承Corgi的信息;陈述但我一直在犯错误:
java:6:错误:找不到符号
科基骗子=新科基(“狩猎”、“什叶派”、“西蒙”,30,7)^ 符号:科氏类 地点:班主
Corgi类中
需要从super()中删除变量
Corgi()中添加值代码>哪个在“主类”中
public static void main(String[] args) {
Corgi tricker = new Corgi("puppy", "Husky", "Alex", 15, 1);
tricker.setTopTrick("Backflip");
System.out.println(tricker);
}
输出-:
DOG DATA
none is a none, a none dog.
The top trick is : Backflip.
The Corgi is 1 years old and weighs 15 pounds.
对不起大家。我的代码不是问题所在。我尝试在另一个编译器中使用该代码,效果很好。我确实根据Kalana的建议对代码做了一些调整。谢谢大家。您的代码似乎不匹配。Corgi没有一个无参数的构造函数,这里有一些问题。1) 您发布的main
中的行与您的错误不匹配。2) Dog()
的构造函数与您在super
中为Corgi
Close调用的构造函数不匹配,但与OP想要的不完全匹配。正确的答案可能是为Dog()
public Corgi(String type, String breed, String name, int pounds, int years) {
// invoke Dog class (super class) constructor
super();
weight = pounds;
age = years;
}
public static void main(String[] args) {
Corgi tricker = new Corgi("puppy", "Husky", "Alex", 15, 1);
tricker.setTopTrick("Backflip");
System.out.println(tricker);
}
DOG DATA
none is a none, a none dog.
The top trick is : Backflip.
The Corgi is 1 years old and weighs 15 pounds.