返回列表 發帖

物件導向基礎概念 (一)

定義一 Car 類別, 並在Car類別下建立三個實體物件, 分別為bus, truck 與 taxi.
執行參考畫面如下:

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. package mhjhmtc;

  2. public class D {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub
  5.       Car truck = new Car();
  6.       
  7.       truck.carName="卡車";
  8.       truck.wheels=6;
  9.       truck.people=3;
  10.       System.out.printf("%s有%d個輪子載%d個人\n\r",truck.carName,truck.wheels,truck.people);
  11.       
  12.       Car bus=new Car();
  13.       
  14.       bus.carName="公車";
  15.       bus.wheels=8;
  16.       bus.people=40;
  17.       System.out.printf("%s有%d個輪子載%d個人\n\r",bus.carName,bus.wheels,bus.people);
  18.       
  19. Car taxi=new Car();
  20.       
  21.       taxi.carName="計程車";
  22.       taxi.wheels=4;
  23.       taxi.people=4;
  24.       System.out.printf("%s有%d個輪子載%d個人\n\r",taxi.carName,taxi.wheels,taxi.people);
  25.         }

  26. }
複製代碼

TOP

  1. package kao;

  2. public class Main
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 Car bus = new Car();
  7.                 bus.carname="公車";
  8.                 bus.wheels=6;
  9.                 bus.people=40;
  10.                 System.out.printf("%s有%d個輪子,可載%d個人\r\n",bus.carname,bus.wheels,bus.people);
  11.                 Car truck = new Car();
  12.                 truck.carname="卡車";
  13.                 truck.wheels=8;
  14.                 truck.people=3;
  15.                 System.out.printf("%s有%d個輪子,可載%d個人\r\n",truck.carname,truck.wheels,truck.people);
  16.                 Car taxi = new Car();
  17.                 taxi.carname="計程車";
  18.                 taxi.wheels=4;
  19.                 taxi.people=5;
  20.                 System.out.printf("%s有%d個輪子,可載%d個人\r\n",taxi.carname,taxi.wheels,taxi.people);
  21.         }
  22. }
複製代碼

TOP

  1. package main;

  2. public class D {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub
  5.       Car truck = new Car();
  6.       
  7.       truck.carName="卡車";
  8.       truck.wheels=6;
  9.       truck.people=3;
  10.       System.out.printf("%s有%d個輪子載%d個人\n\r",truck.carName,truck.wheels,truck.people);
  11.       
  12.       Car bus=new Car();
  13.       
  14.       bus.carName="公車";
  15.       bus.wheels=8;
  16.       bus.people=40;
  17.       System.out.printf("%s有%d個輪子載%d個人\n\r",bus.carName,bus.wheels,bus.people);
  18.       
  19. Car taxi=new Car();
  20.       
  21.       taxi.carName="計程車";
  22.       taxi.wheels=4;
  23.       taxi.people=4;
  24.       System.out.printf("%s有%d個輪子載%d個人\n\r",taxi.carName,taxi.wheels,taxi.people);
  25.         }

  26. }
複製代碼

TOP

  1. package  顏詢大笨蛋1;


  2. import java.util.Scanner;
  3. public class  顏詢大笨蛋2 {

  4.         // 一個專案只會有一個(程式進入點)
  5.         public static void main(String[] args){
  6.                int a =10;
  7.                Car truck =new Car();
  8.                truck.carName="";
  9.                truck.wheels=8;
  10.                truck.people=3;
  11.                system.out.printf ("%s有%b",truck.carname,truck.wheel,truck.people);
  12.                Car bus =new Car();
  13.                truck.carName=" 巴士";
  14.                truck.wheels=6;
  15.                truck.people=40;
  16.                system.out.printf ("%s有%b",taxi.carname,taxi.wheel,taxi.people);
  17.                Car taxi =new Car();
  18.                truck.carName="計程車 ";
  19.                truck.wheels=4;
  20.                truck.people=5;
  21.         }               
  22. }
複製代碼

TOP

  1. package bn.tw;

  2. public class Main {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub
  5.                 Car bus = new Car();
  6.                 bus.carName = "公車";
  7.                 bus.wheels = 6;
  8.                 bus.passengerAmount = 40;
  9.                 System.out.printf("%s有%d個輪子載%d個人\n\r", bus.carName, bus.wheels,
  10.                                 bus.passengerAmount);
  11.                 Car truck = new Car();
  12.                 truck.carName = "卡車";
  13.                 truck.wheels = 8;
  14.                 truck.passengerAmount = 3;
  15.                 System.out.printf("%s有%d個輪子載%d個人\n\r", truck.carName, truck.wheels,
  16.                                 truck.passengerAmount);
  17.                 Car taxi = new Car();
  18.                 taxi.carName = "計程車";
  19.                 taxi.wheels = 4;
  20.                 taxi.passengerAmount = 4;
  21.                 System.out.printf("%s有%d個輪子載%d個人\n\r", taxi.carName, taxi.wheels,
  22.                                 taxi.passengerAmount);
  23.         }

  24. }
複製代碼

TOP

  1. package bbs.istak.org.tw;

  2. public class Main {

  3.         public static void main(String[] args) {
  4.                 Car bus = new Car();
  5.         bus.carname="公車";
  6.         bus.wheels=6;
  7.         bus.people=40;
  8.         System.out.printf("%s有%d個輪子,可載%d個人\r\n",bus.carname,bus.wheels,bus.people);
  9.         Car truck = new Car();
  10.         truck.carname="卡車";
  11.         truck.wheels=8;
  12.         truck.people=3;
  13.         System.out.printf("%s有%d個輪子,可載%d個人\r\n",truck.carname,truck.wheels,truck.people);
  14.         Car taxi = new Car();
  15.         taxi.carname="計程車";
  16.         taxi.wheels=4;
  17.         taxi.people=5;
  18.         System.out.printf("%s有%d個輪子,可載%d個人\r\n",taxi.carname,taxi.wheels,taxi.people);
  19.         }

  20. }
複製代碼
寶寶心裡苦,但寶寶不說。

TOP

  1. public class momo {

  2.         public static void main(String[] args) {
  3.                 // TODO 自動產生的方法 Stub
  4.         Car bus=new Car();
  5.         bus.name="Bus";
  6.         bus.wheel=8;
  7.         bus.load=40;
  8.         bus.output();
  9.         Car truck=new Car();
  10.         bus.name="Truck";
  11.         bus.wheel=6;
  12.         bus.load=3;
  13.         bus.output();
  14.         Car taxi=new Car();
  15.         bus.name="Taxi";
  16.         bus.wheel=4;
  17.         bus.load=4;
  18.         bus.output();
  19.         }

  20. }
  21. class Car
  22. {
  23.         String name;
  24.         int wheel;
  25.         int load;
  26.         void output()
  27.         {
  28.                 System.out.println(this.name+" have "+this.wheel+" wheels,it can load "+this.load+" people.");
  29.         }
  30. }
複製代碼

TOP

返回列表