通过java中的参数访问类的内容?

时间:2016-06-26 20:46:56

标签: java

我需要在这种情况下访问class的内容:ParkingMeter通过另一个类中的另一个方法的参数:public static void checkTheCar(ParkedCar pc, ParkingMeter pm)但不知道如何做到这一点。方法checkTheCar必须从ParkingMeter检索一个整数,并检查它是否在正确的范围内。 任何建议表示赞赏。

public static class ParkingMeter {

    int time;

    public ParkingMeter(int hours,int minutes){
         time = (getMinutes(hours,minutes));

    }
     private int getMinutes(int hours, int minutes){
            int time = (hours * 60) + minutes;
         return time;
        }
}

public static class ParkedCar{

    String CompanyName;             // Company name
    String Model;                   // Car model
    String Color;                   // Car color
    String PlateNumber;             // License plate number
    String Minutes;                 // number of minutes parked 

    public ParkedCar(String companyName, String model, String color,String plate, String minutes){
        CompanyName = companyName;
        Model = model;
        Color = color;
        PlateNumber = plate;
        Minutes = minutes;
    }
 }
public static class PoliceOfficer{
   String OfficerName;
   String BadgeNumber;

   public PoliceOfficer(String name,String badge){
       OfficerName = name;
       BadgeNumber = badge;
   }
   public static void checkTheCar(ParkedCar pc, ParkingMeter pm){
       // do something

   }

   }

2 个答案:

答案 0 :(得分:0)

您可以在greet_with_hi班级

制作公开方法getTime
ParkingMeter

checkTheCar 中你可以这样使用它:

public static class ParkingMeter {

int time;

public int getTime() {
  return time;
}

public ParkingMeter(int hours,int minutes){
     time = (getMinutes(hours,minutes));

}
 private int getMinutes(int hours, int minutes){
    int time = (hours * 60) + minutes;
    return time;
 }}

答案 1 :(得分:0)

班级设计需要一些工作。启动器的4件事... 1)实例数据的封装 - 带有getter / setter的私有实例变量2)在你的变量名上使用camelCase 3)请注意你在停车计时器中设置/访问数据的方式的变化类。 4)在ParkedCar类中添加了一个toString()方法,以便在主程序中打印出结果。

public class Parking {

    public static void main(String[] args) {
        //PoliceOfficer po = new PoliceOfficer("John Policeman", "4599BDG");
        ParkedCar pc = new ParkedCar("Lexus", "IS250", "White", "7GHK111", 90);
        ParkingMeter pm = new ParkingMeter(2, 30);
        PoliceOfficer.checkTheCar(pc, pm);
    }

    public static class ParkingMeter {

        private int time, hours, minutes;

        public ParkingMeter(int hours, int minutes) {
            this.hours = hours;
            this.minutes = minutes;
            setTime(hours, minutes);
        }

        private void setTime(int hours, int minutes) {
            this.time = (hours * 60) + minutes;
        }
        public int getTime() {
            return time;
        }

        public int getHours() {
            return hours;
        }
        public void setHours(int hours) {
            this.hours = hours;
        }

        public int getMinutes() {
            return minutes;
        }
        public void setMinutes(int minutes) {
            this.minutes = minutes;
        }

    }

    public static class ParkedCar {

        private String make; // Company name
        private String model; // Car model
        private String color; // Car color
        private String plateNum; // License plate number
        private int minutes; // number of minutes parked

        public ParkedCar(String make, String model, String color,
            String plate, int minutes) {
            this.make = make;
            this.model = model;
            this.color = color;
            this.plateNum = plate;
            this.minutes = minutes;
        }

        @Override
        public String toString() {
            return color + " " + make + " " + model + ", plateNum=" + plateNum + " ";
        }

    }

    public static class PoliceOfficer {
        private String officerName;
        private String badgeNumber;

        public PoliceOfficer(String name, String badge) {
            this.officerName = name;
            this.badgeNumber = badge;
        }

        public String getOfficerName() {
            return officerName;
        }

        public void setOfficerName(String officerName) {
            this.officerName = officerName;
        }

        public String getBadgeNumber() {
            return badgeNumber;
        }

        public void setBadgeNumber(String badgeNumber) {
            this.badgeNumber = badgeNumber;
        }

        public static void checkTheCar(ParkedCar pc, ParkingMeter pm) {
            System.out.println(pc.toString() + " has been parked for : " + pm.getTime() + " minutes");
        }
    }

}