返回列表 發帖

2022/10/07 課堂重點(若晴)

本帖最後由 葉桔良 於 2022-10-7 21:26 編輯

【課程內容】
基礎物件導向概念(以人為例子)

物件導向基礎概念 (一)
建構子 (一)
物件導向基礎概念 (二)
建構子 (二)
[隨堂測驗] 建構子 (三)

【作業】
基礎物件導向概念(以電腦為例子)
複習今日上課內容
打字練習簿中文第六課
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. public class Main {

  2.         public static void main(String[] args) {
  3.                 // TODO 自動產生的方法 Stub
  4.                 People ye = new People();
  5.                 ye.hd=new Head(20);
  6.                 ye.bd=new Body(30);
  7.                 ye.rh=new Hand(40);
  8.                 ye.lh=new Hand(42);
  9.                 ye.rl=new Leg(453);
  10.                 ye.ll=new Leg(4);
  11.                
  12.                
  13.                 ye.showProfile();
  14.                
  15.                
  16.                
  17.                
  18.         }

  19. }
複製代碼
  1. public class People {
  2.         Head hd=new Head();
  3.         Body bd=new Body();
  4.         Hand rh=new Hand();
  5.         Hand lh=new Hand();
  6.         Leg rl=new Leg();
  7.         Leg ll=new Leg();
  8.        
  9.         void showProfile()
  10.         {
  11.                 System.out.println(hd.circum);
  12.                 System.out.println(bd.capacity);
  13.                 System.out.println(rh.width);
  14.                 System.out.println(lh.width);
  15.                 System.out.println(rl.length);
  16.                 System.out.println(ll.length);
  17.         }
  18. }
複製代碼
  1. public class Body {
  2.         int capacity;
  3.        
  4.         Body()
  5.         {
  6.                 capacity=50;
  7.         }
  8.        
  9.         Body(int capacity)
  10.         {
  11.                 this.capacity=capacity;
  12.         }
  13. }
複製代碼
  1. public class Hand {
  2.         int width;
  3.        
  4.         Hand()
  5.         {
  6.                 width=50;
  7.         }
  8.        
  9.         Hand(int width)
  10.         {
  11.                 this.width=width;
  12.         }

  13. }
複製代碼
  1. public class Head {
  2.         int circum;
  3.        
  4.         Head()
  5.         {
  6.                 circum=50;
  7.         }
  8.        
  9.         Head(int circum)
  10.         {
  11.                 this.circum=circum;
  12.         }
  13. }
複製代碼
  1. public class Leg {
  2.         int length;
  3.        
  4.         Leg()
  5.         {
  6.                 length=50;
  7.         }
  8.        
  9.         Leg(int length)
  10.         {
  11.                 this.length=length;
  12.         }
  13. }
複製代碼

TOP

  1. public class Main {

  2.         public static void main(String[] args) {
  3.                 // TODO 自動產生的方法 Stub
  4.                 Computer ye = new Computer();
  5.                
  6.                 ye.kb = new Keyboard();
  7.                 ye.ms = new Mouse();
  8.                 ye.sc = new Screen();
  9.                 ye.cpu = new Host();
  10.                 ye.ram = new Host();
  11.                 ye.disk = new Host();
  12.                
  13.                 ye.showProfile();
  14.         }

  15. }
複製代碼
  1. public class Computer {
  2.         Keyboard kb = new Keyboard();
  3.         Mouse ms = new Mouse();
  4.         Host cpu = new Host();
  5.         Host ram = new Host();
  6.         Host disk = new Host();
  7.         Screen sc = new Screen();
  8.          void showProfile()
  9.      {
  10.              System.out.println(kb.keys);
  11.              System.out.println(ms.length);
  12.              System.out.println(cpu.frequency);
  13.              System.out.println(ram.speed);
  14.              System.out.println(disk.capacity);
  15.              System.out.println(sc.resolution);
  16.      }
  17.        

  18. }
複製代碼
  1. public class Host {
  2.         int frequency, speed, capacity;
  3.        
  4.         Host()
  5.         {
  6.                 frequency =100;
  7.         }
  8.        
  9.        
  10.        

  11. }
複製代碼
  1. public class Keyboard {
  2.         int keys;
  3.        
  4.         Keyboard()
  5.         {
  6.                 keys = 70;
  7.         }
  8.        
  9.         Keyboard(int keys)
  10.         {
  11.                 this.keys = keys;
  12.         }

  13. }
複製代碼
  1. public class Mouse {
  2.         int length;
  3.        
  4.         Mouse()
  5.         {
  6.                 length = 100;
  7.         }
  8.        
  9.         Mouse(int length)
  10.         {
  11.                 this.length = length;
  12.         }

  13. }
複製代碼
  1. public class Screen {
  2.         int resolution;
  3.        
  4.         Screen()
  5.         {
  6.                 resolution = 300;
  7.         }
  8.        
  9.         Screen(int resolution)
  10.         {
  11.                 this.resolution = resolution;
  12.         }

  13. }
複製代碼

TOP

返回列表