简体   繁体   English

我对Java中的类和对象的概念感到困惑

[英]i have confusion in concept of classes and objects in Java

i have confusion in concept of classes and objects in Java i did the following code but i'm not sure if it right!!! 我对Java中的类和对象的概念感到困惑,我做了以下代码,但是我不确定是否正确! So How can i test it and invoking this class in the main class? 那么我该如何测试它并在主要课程中调用该课程呢?

  import java.util.*;
  public class Pizza {
  public String pizzasize;
  public int cheese;
  public  int pepperoni;
  public Pizza (String pizzasize1,int cheese1,int pepperoni1){
  pizzasize= pizzasize1;
  cheese=cheese1;
  pepperoni=pepperoni1;
 }

this method chick pizza size then declaring and assign pizza cost 此方法将小鸡匹萨的大小然后声明并分配匹萨费用

  public void setPizza(String pizzasize1){
     switch(pizzasize1)
  {
      case "S":
      case "s":   
      {
   int  pc=10;break;
      }
      case "M":
      case "m":   
      {
   int  pc=12;break;
      }
      case "L":
      case "l":   
      {
   int  pc=14;break;
      }
      default:System.out.print("Wrong");
  }//switch
   pizzasize= pizzasize1;
  }

.. ..

 public void setPizza(int cheese1,int pepperoni1){
   cheese=cheese1;
  pepperoni=pepperoni1;
  }

 public String getPizza(){
 return  pizzasize;
 }
 public int getPizza1(){
 return  cheese;
 }
public int getPizza2(){
return  pepperoni;
}

public void calcCost (int pc){
    double totalCost = pc+(cheese+pepperoni) *2;       
}

finally this method for sample output 最后这个方法用于样本输出

public void getDiscriptions(String pizzasize,int cheese,int pepperoni,double totalCost){
Scanner sc=new Scanner(System.in);
System.out.println("place order");
System.out.println("size: L,M,s");
pizzasize=sc.next();
System.out.println("cheese: ");
cheese=sc.nextInt();
System.out.println("pepperoni: ");
pepperoni=sc.nextInt();
System.out.println("");
System.out.println("your order placed is/nlarge pizza with"+cheese+"cheese,"
 +pepperoni+"pepperoni,/ntotal cost is"+totalCost);
}

}//

You've broken your example up into several chunks. 您已将示例分为几个部分。 It seems like all of those methods are supposed to be in the same Pizza class. 似乎所有这些方法都应该在同一个Pizza类中。 Is that true? 真的吗?

The methods look like they belong in a command-line application. 这些方法看起来像它们属于命令行应用程序。 If that's the case, you're going to need a main(...) method. 如果真是这样,您将需要一个main(...)方法。

class Pizza {

    ...the methods you want to test go here...

    public static void main(String[] args) {
        ...your top-level test code goes here...
    }
}

First you must compile it. 首先,您必须对其进行编译。 If you have a command prompt, and you have the JDK installed, you can type this: 如果您有命令提示符,并且已经安装了JDK,则可以键入以下命令:

$ javac Pizza.java

Then, if the compiler gives no error messages, you can run it: 然后,如果编译器未给出任何错误消息,则可以运行它:

$ java Pizza

See the link provided by @PetterFriberg for more info. 有关更多信息,请参见@PetterFriberg提供的链接。

If you want to run it in an Integrated Development Environment (IDE) such as Eclipse or IntelliJ, then that's a whole other question. 如果您想在诸如Eclipse或IntelliJ的集成开发环境(IDE)中运行它,那将是另一个问题。

Suppose, you are Automobile Engineer and you get a contract to built new model car then what will you do to built car ?? 假设您是汽车工程师,并且您获得了建造新模型汽车的合同,那么您将如何制造汽车?

I think, first you will gather information about: 我认为,首先您将收集有关以下信息:

New brand name , Size , Shape , Weight , Color etc. New brand nameSizeShapeWeightColor
Speed , Acceleration , Rotation etc. SpeedAccelerationRotation

After that you will start to design the blueprint of the car. 之后,您将开始设计汽车的蓝图。 The blueprint only shows how it works and how it looks. 该蓝图仅显示其工作方式和外观。 But you never able to feel it in realistic world with that blueprint. 但是您永远无法通过该蓝图在现实世界中感受到它。

To feel the car you have to built it with same feature as mentioned in blueprint. 要感觉到汽车,您必须制造出具有蓝图中提到的相同功能的汽车。 And only after that you can touch it, open the door, ride it, press brake and accelerator. 然后,您才可以触摸它,打开门,骑车,按下制动器和油门。 With the same blueprint you can built car in any number as much as you want. 使用相同的蓝图,您可以建造任意数量的汽车。

In OOP, Class is same as blueprint of car. 在OOP中, Class与汽车的蓝图相同。 It only shows you what car knows like: color , size , weight , height , speed and these are called attributes. 它仅显示您对汽车的了解: colorsizeweightheightspeed ,这些称为属性。 It only tells you how car behaves like: it runs , it stops , it rotates , etc and these are called methods. 它仅告诉您汽车的行为方式:它runsstopsrotates等,这些被称为方法。 Class doesn't exist in real world it's virtual. 类在虚拟世界中并不存在。

class Car_Real {

    String brand_name;
    String color;
    float weight;
    float height;

    void runs() {
        System.out.println("Engine starts");
    }

    void accelerates() {    
        System.out.println("Speed goes on increasing");    
    }

    void brakes(){
        System.out.println("Speed goes on decreasing");    
    }

}

In OOP, Object is real car derived from blueprint.After you create object,you can access the door,you can ride it that means you can feel and access attributes and methods of class. 在OOP中,对象是从蓝图派生的真正的汽车。创建对象后,您可以进入门,可以骑车,这意味着您可以感知和访问类的属性和方法。

public class Car {

    public static void main(String[] args) {    
        Car_Real C1=new Car_Real(); //create object C1 from car    
        C1.runs();    
    }

}

And using same class you can create any number of objects as you want. 使用相同的类,您可以根据需要创建任意数量的对象。

public class Car {

    public static void main(String[] args) {    
        Car_Real C1 = new Car_Real();    
        Car_Real C2 = new Car_Real();    
        C1.runs();    
        C2.brakes(); // create two object C1, C2    
    }

}

Not bad, but a few pointers: 不错,但有几点建议:

switch statements don't work with Strings, they work with byte , short , char , and int primitive data types. switch语句不适用于Strings,它们适用于byteshortcharint基本数据类型。 They also work with enumerated types. 它们还适用于枚举类型。

You could set up an enum with the sizes: 您可以使用以下大小设置一个枚举:

public enum Size {S,M,L};

Then your size field would be of type Size : 那么您的size字段将为Size类型:

private Size pizzaSize;

Make your fields all private instead of public . 将您的字段全部设为private而非public That means they cannot be seen from outside the Pizza class. 这意味着他们无法从Pizza类之外看到。 They can be read using getter methods. 可以使用getter方法读取它们。 If they need to be changed, you can provide setter methods too. 如果需要更改它们,您也可以提供设置方法。

Example: 例:

public class Pizza {

  //cannot be accessed directly from other classes
  private int cheese;

  //allows other classes to read the value, but not change it
  public int getCheese() {
    return cheese;
  }

  //provide a setter like this if you want other classes to be able to change the value.
  public void setCheese(int cheese) {
    this.cheese = cheese;
  }

}

I have omitted the other fields for clarity. 为了清楚起见,我省略了其他字段。 Note also that the getter and setter methods match the field name, except with get and set prepended, and the first letter of the field capitalized. 还要注意,getter和setter方法与字段名称匹配,但带有getset前缀,并且字段的首字母大写。 The compiler does not require this, but it is accepted convention and good practice. 编译器不需要这样做,但是公认的惯例和良好实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM