[英]i have confusion in concept of classes and objects in Java
我对Java中的类和对象的概念感到困惑,我做了以下代码,但是我不确定是否正确! 那么我该如何测试它并在主要课程中调用该课程呢?
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;
}
此方法将小鸡匹萨的大小然后声明并分配匹萨费用
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;
}
最后这个方法用于样本输出
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);
}
}//
您已将示例分为几个部分。 似乎所有这些方法都应该在同一个Pizza
类中。 真的吗?
这些方法看起来像它们属于命令行应用程序。 如果真是这样,您将需要一个main(...)
方法。
class Pizza {
...the methods you want to test go here...
public static void main(String[] args) {
...your top-level test code goes here...
}
}
首先,您必须对其进行编译。 如果您有命令提示符,并且已经安装了JDK,则可以键入以下命令:
$ javac Pizza.java
然后,如果编译器未给出任何错误消息,则可以运行它:
$ java Pizza
有关更多信息,请参见@PetterFriberg提供的链接。
如果您想在诸如Eclipse或IntelliJ的集成开发环境(IDE)中运行它,那将是另一个问题。
假设您是汽车工程师,并且您获得了建造新模型汽车的合同,那么您将如何制造汽车?
我认为,首先您将收集有关以下信息:
New brand name
, Size
, Shape
, Weight
, Color
等
Speed
, Acceleration
, Rotation
等
之后,您将开始设计汽车的蓝图。 该蓝图仅显示其工作方式和外观。 但是您永远无法通过该蓝图在现实世界中感受到它。
要感觉到汽车,您必须制造出具有蓝图中提到的相同功能的汽车。 然后,您才可以触摸它,打开门,骑车,按下制动器和油门。 使用相同的蓝图,您可以建造任意数量的汽车。
在OOP中, Class
与汽车的蓝图相同。 它仅显示您对汽车的了解: color
, size
, weight
, height
, speed
,这些称为属性。 它仅告诉您汽车的行为方式:它runs
, stops
, rotates
等,这些被称为方法。 类在虚拟世界中并不存在。
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");
}
}
在OOP中,对象是从蓝图派生的真正的汽车。创建对象后,您可以进入门,可以骑车,这意味着您可以感知和访问类的属性和方法。
public class Car {
public static void main(String[] args) {
Car_Real C1=new Car_Real(); //create object C1 from car
C1.runs();
}
}
使用相同的类,您可以根据需要创建任意数量的对象。
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
}
}
不错,但有几点建议:
switch
语句不适用于Strings,它们适用于byte
, short
, char
和int
基本数据类型。 它们还适用于枚举类型。
您可以使用以下大小设置一个枚举:
public enum Size {S,M,L};
那么您的size字段将为Size
类型:
private Size pizzaSize;
将您的字段全部设为private
而非public
。 这意味着他们无法从Pizza类之外看到。 可以使用getter方法读取它们。 如果需要更改它们,您也可以提供设置方法。
例:
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;
}
}
为了清楚起见,我省略了其他字段。 还要注意,getter和setter方法与字段名称匹配,但带有get
和set
前缀,并且字段的首字母大写。 编译器不需要这样做,但是公认的惯例和良好实践。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.