简体   繁体   English

java调用方法和对象

[英]java calling methods and objects

my dear Friends I am new to this java ..so plz help me thank u in advance.. Now i am workin on an examples to understand the concepts of java's objects, classes method in detail. 亲爱的朋友,我是这个Java的新手。所以请事先感谢您。.现在,我正在研究一个示例,以详细了解Java对象的概念,类方法。 With all the knowledge i have i tried this example. 以我所掌握的全部知识,我尝试了此示例。 . but i am unable to accomplish the task.. . 但是我无法完成任务。 Can any one plz give me suggestions of corrected coding.. 谁能给我建议改正编码..

I also wanted to know Which s the best site for learning JAVA (like MSDN for .NET ) Thank u in advance 我也想知道哪个是学习JAVA的最佳网站(例如.NET的MSDN),谢谢您

Question:To Create Vehicle having following attributes: Vehicle No., Model, Manufacturer and Color. 问题:要创建具有以下属性的车辆:车辆编号,型号,制造商和颜色。 Create truck which has the following additional attributes: loading capacity( 100 tons…).Add a behavior to change the color and loading capacity. 创建具有以下附加属性的卡车:装载量(100吨…)。添加行为以更改颜色和装载量。 Display the updated truck details. 显示更新的卡车详细信息。

Codings: 值编码:

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/

Here are some general remarks. 以下是一些一般性说明。 Hope they help: 希望他们能帮助:

  1. getters should only return value, they do not have parameters (otherwise they are not getters). 吸气剂应该只返回值,它们没有参数(否则它们不是吸气剂)。
  2. if you're encapsulating fields, make them private 如果要封装字段,请将其设为私有
  3. use descriptive variable names (eg. truck instead of D) 使用描述性的变量名(例如,卡车而不是D)
  4. use descriptive method names ( mainEntry2() tells absolutely nothing) 使用描述性方法名称( mainEntry2()绝对不告诉任何人)
  5. if you're parsing integer from user input, handle parsing exceptions (like NumberFormatException ) 如果您要从用户输入中解析整数,请处理解析异常(例如NumberFormatException

For instance, your program may be re-written like here: 例如,您的程序可以像下面这样重写:

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