[英]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.