forked from hollischuang/toBeTopJavaer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hollis.zhl
committed
Apr 2, 2019
1 parent
f3e94c1
commit b51f059
Showing
5 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
构造函数,是一种特殊的方法。 主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中。 特别的一个类可以有多个构造函数,可根据其参数个数的不同或参数类型的不同来区分它们即构造函数的重载。 | ||
|
||
构造函数跟一般的实例方法十分相似;但是与其它方法不同,构造器没有返回类型,不会被继承,且可以有范围修饰符。构造器的函数名称必须和它所属的类的名称相同。 它承担着初始化对象数据成员的任务。 | ||
|
||
如果在编写一个可实例化的类时没有专门编写构造函数,多数编程语言会自动生成缺省构造器(默认构造函数)。默认构造函数一般会把成员变量的值初始化为默认值,如int -> 0,Integet -> null。 |
15 changes: 15 additions & 0 deletions
15
basics/java-basic/object-oriented-vs-procedure-oriented.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
### 什么是面向过程? | ||
|
||
把问题分解成一个一个步骤,每个步骤用函数实现,依次调用即可。 | ||
|
||
就是说,在进行面向过程编程的时候,不需要考虑那么多,上来先定义一个函数,然后使用各种诸如if-else、for-each等方式进行代码执行。 | ||
|
||
最典型的用法就是实现一个简单的算法,比如实现冒泡排序。 | ||
|
||
|
||
### 什么是面向对象? | ||
将问题分解成一个一个步骤,对每个步骤进行相应的抽象,形成对象,通过不同对象之间的调用,组合解决问题。 | ||
|
||
就是说,在进行面向对象进行编程的时候,要把属性、行为等封装成对象,然后基于这些对象及对象的能力进行业务逻辑的实现。 | ||
|
||
比如想要造一辆车,上来要先把车的各种属性定义出来,然后抽象成一个Car类。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
###什么是多态,多态有什么好处,多态的必要条件是什么、Java中多态的实现方式 | ||
|
||
多态的概念呢比较简单,就是同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果。 | ||
|
||
如果按照这个概念来定义的话,那么多态应该是一种运行期的状态。 | ||
为了实现运行期的多态,或者说是动态绑定,需要满足三个条件。 | ||
|
||
即有类继承或者接口实现、子类要重写父类的方法、父类的引用指向子类的对象。 | ||
|
||
简单来一段代码解释下: | ||
|
||
public class Parent{ | ||
public void call(){ | ||
sout("im Parent"); | ||
} | ||
} | ||
|
||
public class Son extends Parent{// 1.有类继承或者接口实现 | ||
public void call(){// 2.子类要重写父类的方法 | ||
sout("im Son"); | ||
} | ||
} | ||
|
||
public class Daughter extends Parent{// 1.有类继承或者接口实现 | ||
public void call(){// 2.子类要重写父类的方法 | ||
sout("im Daughter"); | ||
} | ||
} | ||
|
||
public class Test{ | ||
public static void main(String[] args){ | ||
Parent p = new Son(); //3.父类的引用指向子类的对象 | ||
Parent p1 = new Daughter(); //3.父类的引用指向子类的对象 | ||
} | ||
} | ||
|
||
这样,就实现了多态,同样是Parent类的实例,p.call 调用的是Son类的实现、p1.call调用的是Daughter的实现。 | ||
有人说,你自己定义的时候不就已经知道p是son,p1是Daughter了么。但是,有些时候你用到的对象并不都是自己声明的啊 。 | ||
比如Spring 中的IOC出来的对象,你在使用的时候就不知道他是谁,或者说你可以不用关心他是谁。根据具体情况而定。 | ||
|
||
|
||
另外,还有一种说法,包括维基百科也说明,动态还分为动态多态和静态多态。 | ||
上面提到的那种动态绑定认为是动态多态,因为只有在运行期才能知道真正调用的是哪个类的方法。 | ||
|
||
还有一种静态多态,一般认为Java中的函数重载是一种静态多态,因为他需要在编译期决定具体调用哪个方法、 | ||
|
||
关于这个动态静态的说法,我更偏向于重载和多态其实是无关的。 | ||
|
||
但是也要看情况,普通场合,我会认为只有方法的重写算是多态,毕竟这是我的观点。但是如果在面试的时候,我“可能”会认为重载也算是多态,毕竟面试官也有他的观点。我会和面试官说:我认为,多态应该是一种运行期特性,Java中的重写是多态的体现。不过也有人提出重载是一种静态多态的想法,这个问题在StackOverflow等网站上有很多人讨论,但是并没有什么定论。我更加倾向于重载不是多态。 | ||
|
||
这样沟通,既能体现出你了解的多,又能表现出你有自己的思维,不是那种别人说什么就是什么的。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### 对于成员变量和方法的作用域,public,protected,private以及不写之间的区别。 | ||
|
||
|
||
- public :表明该成员变量或者方法是对所有类或者对象都是可见的,所有类或者对象都可以直接访问 | ||
- private:表明该成员变量或者方法是私有的,只有当前类对其具有访问权限,除此之外其他类或者对象都没有访问权限.子类也没有访问权限. | ||
- protected:表明成员变量或者方法对类自身,与同在一个包中的其他类可见,其他包下的类不可访问,除非是他的子类 | ||
- default:表明该成员变量或者方法只有自己和其位于同一个包的内可见,其他包内的类不能访问,即便是它的子类 |