如何解决[子类]中的私有访问

时间:2014-06-14 05:17:14

标签: java composition super

我刚开始做java编程,全新的。我正在搜索论坛,并找到了解决这个错误的方法。

我正在使用jdk1.7.0_60,这是我的作业。

我的问题是当我试图编译我的子类时,编译器正在显示 Car.java

error: manufacturer has private access in Vehicle
System.out.println("Manufacturer: +manufacturer); // this error also appear at (Owner :+owner)

但是在Owner.java和vehicle.java中的源代码,它是由问题给出的,所以它不能改变。

这是 owner.java

public class Owner{
private String name;
private String id;

public Owner(String name,String id){
    this.name=name;
    this.id=id;
}

public void display(){
    System.out.println("Owner Name: "+this.name):
    System.out.println("Owner ID: "+this.id);

}
}

这是 vehicle.java

public class Vehicle{
private String manufacturer;
private Owner owner;

public Vehicle(String manufacturer,Owner owner){
    this.manufacturer =manufacturer;
    this.owner=owner;
}

public void display(){
    System.out.println("Manufacturer: "+this.manufacturer);
    this.owner.display();
}
}

这是子类 car.java

public class Car extends Vehicle{
private String model;
private String RegNumber;
private boolean registered = false;

public Car(String manufacturer,Owner owner,String model){
    super(manufacturer,owner);

    this.model=model;

}

public void registerCar(String regNo){
    if (registered = false){

        RegNumber = regNo;
        registered = true;
        }
    else
    {
        System.out.println("Registered");
    }
}

public void display(){
super.display();
    System.out.println("printed in car class");
    System.out.println("Manufacturer: " +manufacturer); //the error
    System.out.println("Owner : " +owner);// and here 
    System.out.println("Model : " +model);
    System.out.println("Registration Number: "+RegNumber);

    }
}//subclass

这是我的主要课程 obj.java

遗憾的是,我的主类也包含错误

import java.util.Scanner;
import java.util.*;

class obj{
public static void main(String [] args){


Scanner sc = new Scanner(System.in);
System.out.print("Insert Owner's name : ");
String ownername=sc.nextLine();
System.out.print("Insert Owner's id : ");
String ownerid=sc.nextLine();   

System.out.print("Enter car manufacturer name : ");
String mname=sc.nextLine();
System.out.print("Enter car model : ");
String carmodel=sc.nextLine();
System.out.print("Enter car registration number : ");
String regnumber=sc.nextLine();

Owner newowner = new Owner(ownername,ownerid);
Car newcar = new Car(mname,ownername,carmodel);

}
}

编译器的错误消息。

C:\Program Files\Java\jdk1.7.0_60\bin>javac obj.java
obj.java:22: error: constructor Car in class Car cannot be applied to given types;
Car newcar = new Car(mname,ownername,carmodel);
         ^
  required: String,Owner,String
  found: String,String,String
  reason: actual argument String cannot be converted to Owner by method invocation conversion

.\Car.java:28: error: manufacturer has private access in Vehicle
            System.out.println("Manufacturer: " +manufacturer);
                                                 ^
.\Car.java:29: error: owner has private access in Vehicle
            System.out.println("Owner : " +owner);

2 个答案:

答案 0 :(得分:0)

我认为您正在寻找制造商的“受保护”访问权限。 “private”不允许子类访问该字段。

答案 1 :(得分:0)

编译器告诉你错误的错误:

  

Car newcar = new Car(mname,ownername,carmodel);

     

必需:String,Owner,String

     

found:String,String,String

当您需要传入类型为所有者的对象时,您传入一个字符串' owernername'

  

。\ Car.java:28:错误:制造商在Vehicle

中拥有私人访问权限
       System.out.println("Manufacturer: " +manufacturer);
     

。\ Car.java:29:错误:所有者在“车辆”中拥有私人访问权限

       System.out.println("Owner : " +owner);

这两个错误是由于Car没有名为' manufacturer'和'所有者'。车辆呢。 '制造商'和'所有者'在车辆中是私人的,因此您无法在Car中访问它们。

允许Car显示其超级车辆制造商'和'所有者'变量,你必须在车辆上写一个公共功能,如“getManufacturer'它返回私有变量'制造商。对'所有者'做同样的事。