diff --git a/README.md b/README.md index 442bb06c..3f81424b 100644 --- a/README.md +++ b/README.md @@ -107,18 +107,21 @@ - [Constructor2](./src/main/java/Basics/Constructor2.java) - [Constructor3](./src/main/java/Basics/Constructor3.java) - [ConstructorDemo1](./src/main/java/Basics/ConstructorDemo1.java) + - Encapsulation - [Encapsulation1](./src/main/java/Basics/Encapsulation1.java) - In short : - hide the things need to hide : users don't need to know how does the library/Class... do the implementation - export the things need to export : users only need to know the how/where (e.g. : `API`) to use the library/Class. - pros : make the code extenable, scalable, easy to maintain + - JavaBean - [CustomerBean](./src/main/java/Basics/CustomerBean.java) - A java class that has below properties - 1. the class is a `public` class - 2. with a `no argument` `public` constructor - 3. has corresponding getter, setter methods + - This - 1. `this` can be used in `attr`, `class`, `method`, `constructor` - 2. `this` on `attr`, `method` @@ -137,6 +140,10 @@ - [thisDemo4](./src/main/java/Basics/thisDemo4.java) - [thisDemo5](./src/main/java/Basics/thisDemo5) + - Extends + - [Extends_demo1](./src/main/java/Basics/Extends_demo1) + - the "children" class can `reuse`, `overwrite` the `attr/method` that their "parent" class already defined + - [JavaHelloWorld](https://github.com/yennanliu/JavaHelloWorld/tree/main/src) : basic3 - Static demo - [staticDemo 1](./src/main/java/Basics/staticDemo1.java) diff --git a/src/main/java/Basics/Extends_demo1/ExtendsTest.java b/src/main/java/Basics/Extends_demo1/ExtendsTest.java new file mode 100644 index 00000000..d8b5ba2a --- /dev/null +++ b/src/main/java/Basics/Extends_demo1/ExtendsTest.java @@ -0,0 +1,20 @@ +package Basics.Extends_demo1; + +// https://www.youtube.com/watch?v=ArTiU9HR5Kw&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=259 + +public class ExtendsTest { + public static void main(String[] args){ + Person p1 = new Person(); + p1.age = 30; + System.out.println("p1.age = " + p1.age); + p1.eat(); + + System.out.println("=================="); + + Student s1 = new Student(); + s1.age = 20; + System.out.println("s1.age = " + s1.age); + s1.sleep(); + s1.study(); + } +} diff --git a/src/main/java/Basics/Extends_demo1/Person.java b/src/main/java/Basics/Extends_demo1/Person.java new file mode 100644 index 00000000..ba80c092 --- /dev/null +++ b/src/main/java/Basics/Extends_demo1/Person.java @@ -0,0 +1,27 @@ +package Basics.Extends_demo1; + +// https://www.youtube.com/watch?v=ArTiU9HR5Kw&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=259 + +public class Person { + // attr + String name; + int age; + + // constructor + public Person(){ + } + + public Person(String name, int age){ + this.name = name; + this.age = age; + } + + // method + public void eat(){ + System.out.println("eat @@@@"); + } + + public void sleep(){ + System.out.println("sleep !!!!!"); + } +} diff --git a/src/main/java/Basics/Extends_demo1/Student.java b/src/main/java/Basics/Extends_demo1/Student.java new file mode 100644 index 00000000..fa10d104 --- /dev/null +++ b/src/main/java/Basics/Extends_demo1/Student.java @@ -0,0 +1,23 @@ +package Basics.Extends_demo1; + +// https://www.youtube.com/watch?v=ArTiU9HR5Kw&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=259 + +public class Student extends Person { + // attr (we can use the attr, method that Person already defined) + String major; + + // constructor + public Student(){ + } + + public Student(String name, int age, String major){ + this.name = name; + this.age = age; + this.major = major; + } + + // method (we can use the attr, method that Person already defined) + public void study(){ + System.out.println("i am study ~~~~"); + } +}