Android 入門 (3)

物件導向與 Java

物件導向的特性

  • Class (類別)
  • Object (物件)
  • Method (方法)
  • Inheritance (繼承)
  • Encapsulation (封裝)

1. Class (類別)

  • 定義了一件事/物的抽象特點
  • 例如:屬性、行為
            
              class Dog { // 類別
                // 狗所擁有的屬性(property)
                protected int legs = 4;
                protected String name;
                protected String sound = "woof";
  
                public void makeSound() { System.out.println(sound); }
                public void setName(String n) { name = n; }
                public void eat(String food) { }
              }
            
          

2. Object (物件)

  • 要使用 Class,必須要先實體化 (Instance)
            
              class Dog {
                protected int legs = 4;
                protected String sound = "woof";              
                // ...
              }
              Dog goby = new Dog(); // Object
            
          

3. Method (方法)

  • 一個 Class 可以做的事
            
              class Dog {
                // ...
                // Method (方法)
                public void makeSound() {
                  System.out.println(sound);
                }
                public void setName(String n) { name = n; }
                public void eat(String food) { }
              }
              Dog goby = new Dog();
              goby.eat("Beef");
            
          

4. Inheritance (繼承)

  • 原本(被繼承)的類別:父類別 (Base class)
  • 子類別 (Drived class / Subclass)
  • 通常會比父類別更加具體化
            
              class Animal {
                protected int age;
                protected int legs;
                protected String sound;
                public Animal(int legs, String sound) {
                  this.legs = legs;
                  this.sound = sound;
                }
                public void makeSound() {
                  System.out.println(sound);
                }
              }
              class Dog extends Animal {
                protected string name;
                public Dog() { super(4, "Woof"); }
                public void setName(String n) { name = n; }
                public void eat(String food) { }
              }
              class Bird extends Animal {
                public Bird() { super(2, "Tweet"); }
              }
              Dog goby = new Dog();
              goby.makeSound(); // Woof
            
          

5. Encapsulation (封裝)

  • 隱藏了某一方法的具體步驟,取而代之的是透過訊息傳遞機制傳送訊息給它
  • 例如:你知道微波爐怎麼啟動,但不需要知道啟動的原理

super()

  • 用途:
    • 讓子類別呼叫父類別的建構式 (constructor)
    • 在子類別透過 super 呼叫那些父類別同名稱的成員
          
            class People {
              private int weight;
              private int height;
              public People (int weight, int height) {
                this.weight = weight;
                this.height = height;
              }
              public void showInfo() {
                System.out.printf("身高:%s\t,體重:%s", this.height, this.weight);
              }
            }
            class Student extends People {
              private score;
              public Student(int height, int weight, int score) {
                super(weight, height);
                this.score = score;
              }
              public showInfo() {
                super.showInfo();
                System.out.print("\t成績:%s", this.score);
              }
            }
          
        

Override (覆寫)

            
              class Animal {
                protected int legs = 1;
                public void getLegs() {
                  System.out.println(legs*4);
                }
              }
              class Bird extends Animal {
                public void getLegs() {
                  System.out.println(legs*2);
                }
              }
              class Main {
                public static void main(String args[]) {
                  Animal dog = new Animal();
                  dog.getLegs(); // 4
                  Bird aBird = new Bird();
                  aBird.getLegs(); // 2
                }
              }
            
          

@Override

  • annotation
  • 可以幫你檢查方法使用是否正確
              
                class Bird extends Animal {
                  @Override
                  public void getLegs() {
                    System.out.println(legs*2);
                  }
                }
              
            

題外話

R.java

  • This is an auto-generated file when you build an Android application.
  • It categorizes the unique IDs into groupings of drawables, strings, and so on.
  • The main purpose of the R.java file is rapid accessibility of resources in the project.
  • As resources are deleted and added to an Android project, this file is updated automatically.

R.java

            
              /* AUTO-GENERATED FILE.  DO NOT MODIFY.
              *
              * This class was automatically generated by the
              * gradle plugin from the resource data it found. It
              * should not be modified by hand.
              */
              package android.support.constraint;
              
              public final class R {
                public static final class attr {
                  public static final int constraintSet = 0x7f020055;
                  public static final int layout_constraintBaseline_creator = 0x7f02008a;
                  public static final int layout_constraintBaseline_toBaselineOf = 0x7f02008b;
                  public static final int layout_constraintBottom_creator = 0x7f02008c;
                  // ...
                public static final class id {
                  public static final int all = 0x7f07001c;
                  public static final int basic = 0x7f07001f;
                  public static final int chains = 0x7f070025;
                  public static final int none = 0x7f070048;
                  // ...
            
          

setContentView(View);

  • This is where you initialize your activity.
          
            protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
            }
          
        

Java 的訪問權限

  • public:   大家都可以存取他
  • private:   只有當前這個 class 可以存取
  • protected:   只有當前這個 class,和繼承他的 class 可以存取
  • default:   不寫的時候,預設是同一個 package 中可以存取

Java 的訪問權限

            
                        | Class | Package | Subclass | Subclass | World
                        |       |         |(same pkg)|(diff pkg)| 
            ————————————+———————+—————————+——————————+——————————+————————
            public      |   +   |    +    |    +     |     +    |   +     
            ————————————+———————+—————————+——————————+——————————+————————
            protected   |   +   |    +    |    +     |     +    |         
            ————————————+———————+—————————+——————————+——————————+————————
            no modifier |   +   |    +    |    +     |          |    
            ————————————+———————+—————————+——————————+——————————+————————
            private     |   +   |         |          |          |    
            
            + : accessible
            blank : not accessible
            
          

資料來源: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html