java关于继承构造函数的小白问题

来源:百度知道 编辑:UC知道 时间:2024/07/03 00:43:44
class Vehicle{
public static int wheels;
public static int weight;
public Vehicle(int a,int b)
{
wheels=a;
weight=b;
}
public void display(){
System.out.println("wheels:"+wheels);
System.out.println("weight:"+weight);
}

}
class Car extends Vehicle{
public int loader;
public Car(int a,int b,int c){
super(wheels,weight);//wrong
this.loader=c;
public void display(){//wrong
super.display();
System.out.println("loader:"+loader);
}
}
}

public class DK{
public static void main(String[] args) {
Vehicle p1=new Vehicle(3,4);
Vehicle p2=new Vehicle(1,2);
Car s1=new Car(4,5,6);
Car s2=new Car(1,2,3);
p1.display();
p2.display();
}
}
提示错误的行我标出来了

你大括号打错了:类car中将11行的括号放到第五行之后六行之前
1class Car extends Vehicle{
2 public int loader;
3 public Car(int a,int b,int c){
4 super(wheels,weight);//wrong
5 this.loader=c;
6 public void display(){//wrong
7 super.display();
8 System.out.println("loader:"+loader);
9 }
10}
11}
改后正确的:
1class Car extends Vehicle{
2 public int loader;
3 public Car(int a,int b,int c){
4 super(wheels,weight);//wrong
5 this.loader=c;
11 }

6 public void display(){//wrong
7 super.display();
8 System.out.println("loader:"+loader);
9 }
10}

class Vehicle{
public static int wheels;
public static int weight;
public Vehicle(int a,int b)
{
wheels=a;
weight=b;
}
public void display(){
System.out.println("wheels:"+wheels);
System.out.println("wei