簡體   English   中英

java調用方法和對象

[英]java calling methods and objects

親愛的朋友,我是這個Java的新手。所以請事先感謝您。.現在,我正在研究一個示例,以詳細了解Java對象的概念,類方法。 以我所掌握的全部知識,我嘗試了此示例。 但是我無法完成任務。 誰能給我建議改正編碼..

我也想知道哪個是學習JAVA的最佳網站(例如.NET的MSDN),謝謝您

問題:要創建具有以下屬性的車輛:車輛編號,型號,制造商和顏色。 創建具有以下附加屬性的卡車:裝載量(100噸…)。添加行為以更改顏色和裝載量。 顯示更新的卡車詳細信息。

值編碼:

import java.io.*;

class Vehicle
{
    String VehicleNo;
    String Model;
    String Manufacturer;
    String Color;


    public void setNo(String VehicleNo)
    {
    this.VehicleNo=VehicleNo;
    }

    public String getNo()
    {
    return VehicleNo;
    }


    public void setModel(String Model)
    {
    this.Model=Model;
    }

    public String getModel()
    {
    return Model;
    }

    public void setManufacturer(String Manufacturer)
    {
    this.Manufacturer=Manufacturer;
    }

    public String getManufacturer()
    {
    return Manufacturer;
    }



    public void setColor(String Color)

    {
    this.Color=Color;
    }

    public String getColor(String s)
    {
    return s;
    }

}


class Truck extends Vehicle

{

    double LoadingCapacity;

    public void setLoad(double LoadingCapacity)
    {
    this.LoadingCapacity=LoadingCapacity;
    }


    public double getLoad(double ld)
    {
    return ld;
    }
}

public class VehicleInfo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void mainEntry2() throws IOException
    {   
        //Truck D=new Truck();
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Color: ");
        String col=br.readLine();
        D.setColor(col);

        System.out.print("Enter Loading Capacity: Maximum 100tons");
        int load=Integer.parseInt(br.readLine());
        D.setLoad(load);
    }
    public static void main(String[] args) throws IOException 
        {
            int loop_option=0;
            public Truck D=new Truck();
            public BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Vehicle No: ");
            String no=br.readLine();
            D.setNo(no);

            System.out.print("Model: ");
            String model=br.readLine();
            D.setModel(model);

            System.out.print("Manufacturer: ");
            String man=br.readLine();
            D.setManufacturer(man);

mainEntry2();
            do
            {
                System.out.println("");

                System.out.println("----Vehicle Information----");
                System.out.println();
                System.out.println("Vehicle No: "+D.getNo());
                System.out.println("Model: "+D.getModel());
                System.out.println("Manufacturer: "+D.getManufacturer());
                System.out.println("Color: "+D.getColor(col));
                System.out.println("Loading Capacity: "+D.getLoad(load));
                System.out.println("");
                System.out.println(" if U want to update color and loading capacity press 1 and to exit press 2 ..");
                loop_option=Integer.parseInt(br.readLine());
                if(loop_option==1)
                {
                    mainEntry2();
                }

            }while(loop_option==1);

        }

    }

您應該從這里開始學習Java: http : //download.oracle.com/javase/tutorial/

以下是一些一般性說明。 希望他們能幫助:

  1. 吸氣劑應該只返回值,它們沒有參數(否則它們不是吸氣劑)。
  2. 如果要封裝字段,請將其設為私有
  3. 使用描述性的變量名(例如,卡車而不是D)
  4. 使用描述性方法名稱( mainEntry2()絕對不告訴任何人)
  5. 如果您要從用戶輸入中解析整數,請處理解析異常(例如NumberFormatException

例如,您的程序可以像下面這樣重寫:

import java.io.*;

class Vehicle {
    private String vehicleNo;
    private String model;
    private String manufacturer;
    private String color;

    public void setNo(String vehicleNo) {
        this.vehicleNo = vehicleNo;
    }

    public String getNo() {
        return vehicleNo;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setColor(String color)
    {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

}

class Truck extends Vehicle
{
    private double loadingCapacity;

    public void setLoad(double loadingCapacity) {
        this.loadingCapacity = loadingCapacity;
    }

    public double getLoad() {
        return this.loadingCapacity;
    }

}

public class VehicleInfo {

    private static void updateColorAndCapacity(BufferedReader br, Truck truck)
        throws IOException
    {
        System.out.print("Color: ");
        String col = br.readLine();
        truck.setColor(col);

        while (true)
        {
            System.out.print("Enter Loading Capacity (maximum 100tons):");
            String value = br.readLine();
            value = value.trim();
            try {
                int load = Integer.parseInt(value);
                truck.setLoad(load);
                return;
            } catch (NumberFormatException e) {
                /// do it once again
                continue;
            }
        }
    }

    public static void main(String[] args) 
        throws IOException {

        Truck truck = new Truck();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Vehicle No: ");
        String no = br.readLine();
        truck.setNo(no);

        System.out.print("Model: ");
        String model = br.readLine();
        truck.setModel(model);

        System.out.print("Manufacturer: ");
        String man = br.readLine();
        truck.setManufacturer(man);

        updateColorAndCapacity(br, truck);

        int loop_option = 0;        
        do {
            System.out.println();
            System.out.println("----Vehicle Information----");
            System.out.println();
            System.out.println("Vehicle No: " + truck.getNo());
            System.out.println("Model: " + truck.getModel());
            System.out.println("Manufacturer: " + truck.getManufacturer());
            System.out.println("Color: " + truck.getColor());
            System.out.println("Loading Capacity: " + truck.getLoad());
            System.out.println();
            System.out.println(" if U want to update color and loading capacity press 1 and to exit press 2 ..");

            loop_option = Integer.parseInt(br.readLine());

            if (loop_option == 1) {
                updateColorAndCapacity(br, truck);
            }

        } while (loop_option == 1);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM