物件導向的特性
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 Dog {
protected int legs = 4;
protected String sound = "woof";
// ...
}
Dog goby = new Dog(); // Object
3. Method (方法)
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 (繼承)
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()
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
class Bird extends Animal {
@Override
public void getLegs() {
System.out.println(legs*2);
}
}
題外話
R.java
R.java file is rapid accessibility of resources in the project.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);
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