从前端到全栈 -- 最全面向对象总结

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从事前端的这两年中,也接触了后端领域,像 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"PHP","attrs":{}}],"attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Node","attrs":{}}],"attrs":{}},{"type":"text","text":", 并且都用它们做过一些 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"demo","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"私活","attrs":{}}],"attrs":{}},{"type":"text","text":" ,站在职业发展道路上来讲,了解后端是好的,自己可以熟练前后的开发流程,整条链路下来,很清晰,懂了后端,在自己以后创业或者接私活,都是不错的。 而且现在在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Web","attrs":{}}],"attrs":{}},{"type":"text","text":"后台领域, ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java","attrs":{}}],"attrs":{}},{"type":"text","text":" 是 老大哥了,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Spring","attrs":{}}],"attrs":{}},{"type":"text","text":" 全家桶走遍天下,于是最近又重拾 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java","attrs":{}}],"attrs":{}},{"type":"text","text":" 了,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"从前端迈向全栈","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这是我最近花了一周的时间覆盘的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java 面向对象","attrs":{}}],"attrs":{}},{"type":"text","text":" 总结,希望能帮助到你.","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"导读目录","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/af/af4dade71d807ba26e6f1fbbafc368dc.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"学习路线","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这份学习路线 是 我跟着 某up主 视频总结的,很全面","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e5/e57643c3ff95cd8cbc0f2da6c6fdef6b.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"重学Java","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"一、面向对象 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"OOP","attrs":{}}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1. 1 关于 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 区别","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" 调用父类的构造方法,必须在子类构造方法中第一个","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" 必须只能出现在子类的方法 或者 构造方法 中","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":" this","attrs":{}}],"attrs":{}},{"type":"text","text":" 不能同时出现在调用构造方法","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代表的对象不同:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" : 本身调用者这个对象","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" : 代表父类对象应用","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.2 多态","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同一方法可以根据发送对象的不同而采用多种不同的行为方式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一个对象的实际类型是确定的,但可以指向对象的引用类型有很多。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"多态存在条件","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"有继承关系","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"子类重写父类的方法","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"父类引用指向了子类对象","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"注意","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"子类如果重写了父类方法,父类对象是调用的子类重写后的方法","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"类型转换","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"类型转换目的: ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"方便方法调用,降级,升级,减少代码重复性,但也有缺点,丢失精度性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"向上转型 (子 转 父 ) 低转高 可直接转","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"向下转型 (父 转 子) 高转低 需要强制转换","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"高 转 低 后, 然后再进行强制转换,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"子类型 子对象 = (子类型) 父对象","attrs":{}}],"attrs":{}}]}],"attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":" Person per = new Student()\n Student student = (Student) per // ((Student) per).方法/属性\n \n per.子属性和方法\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.3 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.1 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 用途","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在没有创建对象的情况下调用方法和属性(静态的)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 可以用来修饰类的成员方法、类的成员变量,另外可以编写static代码块来优化程序性能。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.2 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 静态方法","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 方法一般称作静态方法,静态方法不依赖于任何对象就可以进行访问, 因此对于静态方法来说,是没有this的。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"在静态方法中","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"不能访问类的非静态成员变量和非静态成员方法","attrs":{}},{"type":"text","text":",因为非静态成员方法/变量都是必须依赖具体的对象才能够被调用。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"在非静态方法中是可以访问静态成员方法/变量的。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.3 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 静态变量","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"静态变量和非静态变量的区别是","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"静态变量被所有的对象所共享,在内存中只有一个副本,它当且仅当在类初次加载时会被初始化。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"非静态变量是对象所拥有的,在创建对象的时候被初始化,存在多个副本,各个对象拥有的副本互不影响。","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"static成员变量的初始化顺序按照定义的顺序进行初始化。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.4 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 代码块","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"关键字来形成静态代码块以优化程序性能,。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"块可以置于类中的任何地方,类中可以有多个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"块。在类初次被加载的时候,会按照","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"块的顺序来执行每个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"块,并且只会执行一次。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"可以把方法中常量放入到 静态块中,这样不至于,每次调用,又重新创建,造成空间浪费。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class Test {\n private static Date date ;\n private static SimpleDateFormat formatter;\n static {\n formatter= new SimpleDateFormat(\"yyyy-MM-dd 'at' HH:mm:ss z\");\n date = new Date(System.currentTimeMillis());\n }\n public static void main(String[] args) {\n getTime();\n }\n public static void getTime(){\n System.out.println(formatter.format(date));\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上例子是 用来获取当前时间,在调用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"getTime","attrs":{}}],"attrs":{}},{"type":"text","text":" 静态方法时,它不会重复创建","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Date","attrs":{}}],"attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SimpleDateFormat","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"执行顺序","attrs":{}},{"type":"text","text":": 优于构造器","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.5 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 注意","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"关键字不能会改变类中成员的访问权限, 能改变访问权限只有 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"private","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"protected","attrs":{}}],"attrs":{}},{"type":"text","text":".","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"通过 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 可以在非静态方法中是可以访问静态方法和静态成员的","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":" public class Test {\n private static Date date ;\n private static SimpleDateFormat formatter;\n private static String name = \"前端自学社区\";\n \n public static void main(String[] args) {\n Test test = new Test();\n test.getStatic();\n }\n public static void func(){\n System.out.println(\"我是静态方法\");\n }\n public void getStatic(){\n System.out.println(this.name);\n this.func();\n }\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例子中,在非静态方法中,可以直接通过 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 来进行访问的静态方法和静态成员 ,然后通过类实例化后,对象即可通过非静态方法来获取到静态方法中得静态属性和方法。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.4 抽象类 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"abstract","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们先来了解一下抽象方法,抽象方法是一种特殊的方法:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"它只有声明,而没有具体的实现","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"抽象方法的声明格式为:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"abstract int getNum();\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.4.1何为 抽象类 呢 ?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"被 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"abstract","attrs":{}}],"attrs":{}},{"type":"text","text":"关键词修饰的类就是抽象类,抽象类含有属性和抽象方法。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"抽象类中含有无具体实现的方法,所以不能用抽象类创建对象。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当 子类 继承 了 抽象类 ,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"那么必须的实现抽象类的抽象方法,同时 子类的实例化对象拥有抽象类的属性","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public abstract class Animal {\n abstract String name;\n abstract void eat();\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.4.2 抽象类作用?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"抽象类主要用来抽离事物的共性,抽离出属性和方法,子类 继承 抽象类,提高代码效率。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.4.3 抽象类与普通类区别","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"抽象方法必须为public或者protected(因为如果为private,则不能被子类继承,子类便无法实现该方法),缺省情况下默认为public。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"抽象类不能用来创建对象;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"如果一个类继承于一个抽象类,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"则子类必须实现父类的抽象方法","attrs":{}},{"type":"text","text":"。如果子类没有实现父类的抽象方法,则必须将子类也定义为为abstract类。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.5 接口 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"interface","attrs":{}}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.5.1 什么是接口?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它是对行为的抽象,接口通常以","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"interface","attrs":{}}],"attrs":{}},{"type":"text","text":"来声明。一个类通过继承接口的方式,从而来继承接口的抽象方法。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当一个类需要用到这些抽象行为的话,需要 实现接口 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"implements","attrs":{}}],"attrs":{}},{"type":"text","text":" , 而且必须实现接口中方法。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public interface person {\n void study(){} \n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.5.2 接口特性","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"接口中每一个方法也是隐式抽象的,接口中的方法会被隐式的指定为 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public abstract","attrs":{}}],"attrs":{}},{"type":"text","text":"(只能是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public abstract","attrs":{}}],"attrs":{}},{"type":"text","text":",其他修饰符都会报错)。 也可不写,默认就是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public abstract","attrs":{}}],"attrs":{}},{"type":"text","text":" 。","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":" public interface Aniaml{\n public abstract void eat(); // 抽象方法 == 等同于 void eat();\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":2,"normalizeStart":2},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"接口中可以含有变量,但是接口中的变量会被隐式的指定为 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public static final","attrs":{}}],"attrs":{}},{"type":"text","text":" 变量(并且只能是 public,用 private 修饰会报编译错误)。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"接口中的方法是不能在接口中实现的,只能由实现接口的类来实现接口中的方法。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.5.4 接口的作用","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"使用接口可以继承超过一个以上的来源,这样可以为不同的需求组合出不同的继承层次。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"接口规范了实现类,必须实现的方法和属性。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.6 接口与抽象类","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Java中,可以通过两种形式来体现","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"OOP","attrs":{}}],"attrs":{}},{"type":"text","text":"的抽象:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"接口","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"抽象类","attrs":{}}],"attrs":{}},{"type":"text","text":", 它俩有很多相似之处。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.6.1 接口与抽象 区别","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"抽象类可以提供成员方法的实现细节,而接口中只能存在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public abstract ","attrs":{}}],"attrs":{}},{"type":"text","text":"方法;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"抽象类中的成员变量可以是各种类型的,而接口中的成员变量只能是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public static final","attrs":{}}],"attrs":{}},{"type":"text","text":"类型的;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"接口中不能含有静态代码块以及静态方法,而抽象类可以有静态代码块和静态方法;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"一个类只能继承一个抽象类,而一个类却可以实现多个接口。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"interface","attrs":{}}],"attrs":{}},{"type":"text","text":" 接口 是针对事物的 行为 [ 方法 ] 来进行抽象的, 而 抽象类 是针对事物的共性来 对 行为 和 属性 来进行抽象的。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.6.2 例子","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"接口 interface","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public interface PeopleInterface {\n void eat();\n void study();\n void play();\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"抽象类 abstract","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public abstract class People {\n String name = \"人类\";\n abstract void watch();\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"子类","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class Test extends People implements PeopleInterface {\n public static void main(String[] args) {\n Test test = new Test(); // 子类拥有抽象类的属性\n System.out.println(test.name);\n test.eat();\n }\n\n @Override\n public void eat() {\n System.out.println(\"吃的功能\");\n }\n\n @Override\n public void study() {\n System.out.println(\"学习功能\");\n }\n\n @Override\n public void play() {\n System.out.println(\"玩的功能\");\n }\n\n @Override\n void watch() {\n System.out.println(\"看的功能\");\n }\n\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面例子中讲了 类 与 接口 抽象类之间关系。 首先 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":" Test","attrs":{}}],"attrs":{}},{"type":"text","text":" 类 继承了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"People","attrs":{}}],"attrs":{}},{"type":"text","text":" 抽象类,然后又 实现了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"PeopleInterface","attrs":{}}],"attrs":{}},{"type":"text","text":" 接口。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"子类继承 抽象类的同时,必须实现抽象类的抽象方法,子类实例化时,拥有抽象类的属性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"子类实现 接口的同时, 必须实现接口中所有的接口行为。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"结语","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上是对","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java 面向对象","attrs":{}}],"attrs":{}},{"type":"text","text":" 的总结分享,希望能对你有所启发,欢迎提出意见!","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章