java调用方法和对象

时间:2010-12-28 20:38:03

标签: java

我亲爱的朋友我是这个java的新手..所以请帮助我提前感谢你..现在我正在研究一个例子,以便详细了解java的对象,类方法的概念。 凭借我所有的知识,我尝试了这个例子。 。但我无法完成任务......可以任何一个PLZ给我建议纠正编码..

我也想知道哪个是学习JAVA的最佳网站(比如MSDN for .NET) 提前谢谢你

问题:创建具有以下属性的车辆:车辆编号,型号,制造商和颜色。创建具有以下附加属性的卡车:装载能力(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);

        }

    }

2 个答案:

答案 0 :(得分:2)

你应该从这里开始学习Java:http://download.oracle.com/javase/tutorial/

答案 1 :(得分:2)

以下是一些一般性评论。希望他们帮忙:

  1. getters应该只返回值,它们没有参数(否则它们不是getter)。
  2. 如果您要封装字段,请将其设为私有
  3. 使用描述性变量名称(例如,卡车而不是D)
  4. 使用描述性方法名称(mainEntry2()绝对没有说明)
  5. 如果从用户输入解析整数,则处理解析异常(如NumberFormatException
  6. 例如,您的程序可能会像这里一样重写:

    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);
        }
    }
    
相关问题