Skip to content

Commit

Permalink
add extends demo 1
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Apr 27, 2021
1 parent a432196 commit 4da4bf4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/Basics/Extends_demo1/ExtendsTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
27 changes: 27 additions & 0 deletions src/main/java/Basics/Extends_demo1/Person.java
Original file line number Diff line number Diff line change
@@ -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 !!!!!");
}
}
23 changes: 23 additions & 0 deletions src/main/java/Basics/Extends_demo1/Student.java
Original file line number Diff line number Diff line change
@@ -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 ~~~~");
}
}

0 comments on commit 4da4bf4

Please sign in to comment.