我必须完成 Java方案,他说:
撰写“java”方案,以实施继承。 您的方案应有以下结构:
- Create a Base class to hold two Integers and a method to Display them.
- In the derived Class add another Integer and Display it.
- Create a method in the same derived Class to add the three numbers.
- Pass the values to the integers and Display the results.
我知道,在 Java,我不知道我做什么是正确还是错的,还是什么! 我认为,我不理解所希望的东西。 这里我要谈谈:
public class firstclass {
int a=5;
int b=6;
public void Display (){
System.out.println(a+b);
}
}
public class secondclass extends firstclass {
int z=0;
public void Displaysecond (){
System.out.println(z);
}
public void add (){
z=a+b;
System.out.println(z);
}
}
public class mainOne {
public static void main(String[] args) {
firstclass call = new firstclass();
secondclass call2 = new secondclass();
call.Display();
call2.Displaysecond();
call2.add();
}
}
It runs without any problems but I get "11" for the "System.out.println(a+b);" while a = 5 and b = 6.
Am I going about this correctly?