为什么不应用我的私人修饰符?

问题描述

因此,我一直在阅读在线教程,并将其分为公共和私人课程。我理解它,但是在学习本教程时,我遇到了一个问题,我要么无法运行该程序,因为它不允许我使用非静态类,或者我使用静态类运行它,而我的私有变量仍然可以在主要方法中进行更改。作为参考,我包括了我的代码的静态版本以及我所引用的视频。

https://www.youtube.com/watch?v=psL2NHg8eQs&list=PLnVYEpTNGNtVVLAa3GH_jPLoNLFBjYkHz&index=23

import java.util.Scanner;
import java.text.Format;
public class Tutorial13 {
    public static void main(String[] args){
        Scanner input= new Scanner(System.in);
        //creating new objects with constructors
        //start with class,then the name of the object,then new followed by the values required by the constructor.
        aircraft Cessna172 = new aircraft(4,140,56.5,9.5);
        aircraft PiperSaratoga = new aircraft(6,201,102.5,20.5);

        //create a few objects of cars
        car Corvette = new car(2,184,18.5,4.6);
        car HondaFit = new car(5,115,10.6,2.8);

        //Display vehicle info
        System.out.println("Vehicle info:");
        System.out.println("-------------------------------------------------------------");
        System.out.println("Cessna172");
        Cessna172.info();
        System.out.println("Piper Saratoga");
        PiperSaratoga.info();
        System.out.println("Corvette");
        Corvette.info();
        System.out.println("Honda Fit");
        HondaFit.info();
        System.out.println("-------------------------------------------------------------");

        System.out.println("Tests");

        System.out.println("The Cessna 172 can fly for " + Cessna172.aircraftEndurance()+ " hours");
        System.out.println("Piper Saratoga can fly for " + PiperSaratoga.aircraftEndurance()+ " hours");
        System.out.println("The Corvette can drive for " + Corvette.carEndurance()+ " hours");
        System.out.println("The Honda Fit can drive for " + HondaFit.carEndurance()+ " hours");
        System.out.println("If you flew the Cessna 172 for 2.5 hours,it would take " + Cessna172.fuelNeeded(2.5)+ " gallons of fuel.");
        System.out.println("If you drove the Corvette for 2.5 hours,it would take "+ Corvette.fuelNeeded(2.5)+ " gallons of fuel.");

        **//Change the value of the number of passengers to 10
        //This should not work because it is set to private (But it still does,not sure why)
        Cessna172.setPassengers(50);
        System.out.println("Cessna 172 now contains " +Cessna172.passengers+ " passengers.");**


    }
    //without the phrase public or static,Java applies the public modifier by default
    //When you add the private modifier,only methods and code within the designated class can change the code.


    static class aircraft{
        **private int passengers,cruiseSpeed;
        private double  fuelCapacity,fuelBurnRate;**

        //constructor
        aircraft(int p,int c,double fc,double fbr){
            passengers= p;
            cruiseSpeed= c;
            fuelCapacity= fc;
            fuelBurnRate= fbr;
        }

        //method to calculate Endurance
        double aircraftEndurance(){
            double endurance = fuelCapacity/fuelBurnRate;
            return endurance;
        }
        //Method to determine how much fuel is required to use this vehicle for a set period of time.
        double fuelNeeded(double time){
            return fuelBurnRate/time;
        }
        void info(){
            System.out.println("-------------------------------------------------------------");
            System.out.println("# of passengers\tspeed\tfuel cap.\t\tfuel burn rate");
            System.out.println(passengers+" passengers\t"+cruiseSpeed+"mph\t"+fuelCapacity+" Gallons\t"+fuelBurnRate+" Gallons per hour");
            System.out.println("-------------------------------------------------------------");
        }
        //By doing this within the class,this method will be the only way to change the private value of the passengers Data.
        void setPassengers(int p){
            if((p>0)&&(p<=10)){
                passengers = p;
                System.out.println("Revised passenger count: " + passengers);
            }
            else System.out.println("Error setting passengers.");
        }

    }
    static class car{
        **private int passengers,fuelBurnRate;**
        car(int p,double fbr){
            passengers=p;
            cruiseSpeed=c;
            fuelCapacity = fc;
            fuelBurnRate = fbr;
        }
        double carEndurance(){
            double endurance = fuelCapacity/fuelBurnRate;
            return endurance;
        }
        double fuelNeeded(double time){
            return fuelBurnRate/time;
        }
        void info(){
            System.out.println("-------------------------------------------------------------");
            System.out.println("# of passengers\tspeed\tfuel cap.\t\tfuel burn rate");
            System.out.println(passengers+" passengers\t"+cruiseSpeed+"mph\t"+fuelCapacity+" Gallons\t"+fuelBurnRate+" Gallons per hour");
            System.out.println("-------------------------------------------------------------");
        }
        void setPassengers(int p){
            if((p>0)&&(p<=10)){
                passengers = p;
                System.out.println("Revised passenger count: "+ passengers);
            }
            else System.out.println("Error setting passengers.");
        }
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...