Skip to content

Commit 4da4bf4

Browse files
committed
add extends demo 1
1 parent a432196 commit 4da4bf4

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,21 @@
107107
- [Constructor2](./src/main/java/Basics/Constructor2.java)
108108
- [Constructor3](./src/main/java/Basics/Constructor3.java)
109109
- [ConstructorDemo1](./src/main/java/Basics/ConstructorDemo1.java)
110+
110111
- Encapsulation
111112
- [Encapsulation1](./src/main/java/Basics/Encapsulation1.java)
112113
- In short :
113114
- hide the things need to hide : users don't need to know how does the library/Class... do the implementation
114115
- export the things need to export : users only need to know the how/where (e.g. : `API`) to use the library/Class.
115116
- pros : make the code extenable, scalable, easy to maintain
117+
116118
- JavaBean
117119
- [CustomerBean](./src/main/java/Basics/CustomerBean.java)
118120
- A java class that has below properties
119121
- 1. the class is a `public` class
120122
- 2. with a `no argument` `public` constructor
121123
- 3. has corresponding getter, setter methods
124+
122125
- This
123126
- 1. `this` can be used in `attr`, `class`, `method`, `constructor`
124127
- 2. `this` on `attr`, `method`
@@ -137,6 +140,10 @@
137140
- [thisDemo4](./src/main/java/Basics/thisDemo4.java)
138141
- [thisDemo5](./src/main/java/Basics/thisDemo5)
139142
143+
- Extends
144+
- [Extends_demo1](./src/main/java/Basics/Extends_demo1)
145+
- the "children" class can `reuse`, `overwrite` the `attr/method` that their "parent" class already defined
146+
140147
- [JavaHelloWorld](https://github.com/yennanliu/JavaHelloWorld/tree/main/src) : basic3
141148
- Static demo
142149
- [staticDemo 1](./src/main/java/Basics/staticDemo1.java)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package Basics.Extends_demo1;
2+
3+
// https://www.youtube.com/watch?v=ArTiU9HR5Kw&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=259
4+
5+
public class ExtendsTest {
6+
public static void main(String[] args){
7+
Person p1 = new Person();
8+
p1.age = 30;
9+
System.out.println("p1.age = " + p1.age);
10+
p1.eat();
11+
12+
System.out.println("==================");
13+
14+
Student s1 = new Student();
15+
s1.age = 20;
16+
System.out.println("s1.age = " + s1.age);
17+
s1.sleep();
18+
s1.study();
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Basics.Extends_demo1;
2+
3+
// https://www.youtube.com/watch?v=ArTiU9HR5Kw&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=259
4+
5+
public class Person {
6+
// attr
7+
String name;
8+
int age;
9+
10+
// constructor
11+
public Person(){
12+
}
13+
14+
public Person(String name, int age){
15+
this.name = name;
16+
this.age = age;
17+
}
18+
19+
// method
20+
public void eat(){
21+
System.out.println("eat @@@@");
22+
}
23+
24+
public void sleep(){
25+
System.out.println("sleep !!!!!");
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Basics.Extends_demo1;
2+
3+
// https://www.youtube.com/watch?v=ArTiU9HR5Kw&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=259
4+
5+
public class Student extends Person {
6+
// attr (we can use the attr, method that Person already defined)
7+
String major;
8+
9+
// constructor
10+
public Student(){
11+
}
12+
13+
public Student(String name, int age, String major){
14+
this.name = name;
15+
this.age = age;
16+
this.major = major;
17+
}
18+
19+
// method (we can use the attr, method that Person already defined)
20+
public void study(){
21+
System.out.println("i am study ~~~~");
22+
}
23+
}

0 commit comments

Comments
 (0)