目錄html
第4章 對象和類
4.2 Java類
1.類決定了對象。
2.代碼清單4.1 Employee類java
class Employee{ int age; double salary; }
3.一個公有類的定義必須保存在一個文件中,這個文件名和類名相同,文件名必須以java爲擴展名。
4.代碼清單4.2 帶有構造方法的Employee類android
public class Employee{ public int age; public double salary; public Employee(){ } public Employee(int ageValue, double salaryValue){ age=ageValue; salary=salaryValue; } }
4.7 封裝和訪問控制
4.7.1 類訪問控制修飾符
1.一個Java源文件只能包含一個public類
2.代碼清單4.5 Chapter類,具備默認的訪問級別git
package app04; class Chapter{ String title; int numberOfPages; public void review(){ Page page =new Page(); int sentenceCount=page.numberOfSentences; int pageNumber=page.getPageNumber(); } }
3.類成員的訪問級別
app
4.8 this關鍵字
1.若是有一個類級別的字段,它和一個局部變量具備相同的名字:用this.field來應用,一般用來接收用於初始化字段的值。
2.代碼清單4.7 Box類ide
package app04; public class Box{ int length; int width; int height; public Box(int length,int width,int height){ this.length=length; this.width=width; this.height=height; } }
4.9 使用其餘的類
1.Java提供了關鍵字import,表示想要用一個包或者來自包中的一個類。好比,要在本身的代碼中使用java.util.ArrayList,必須使用以下的import語句:學習
import java.util.ArrayList;
4.11 靜態成員
1.從一個靜態方法中不能調用非靜態成員。
4.16 靜態工廠方法
1.代碼清單4.11 Discount類測試
package app04; import java.time.LocalDate; public class Discount{ private int value; private Discount(int value){ this.value=value; } public int getValue(){ return this.value; } public static Discount createSmallCustomerDiscount(){ return new Discount(10); } public static Discount createBigCustomerDiscount(){ return new Discount(12); } }
4.17 傳值或傳引用
1.代碼清單4.12 ReferencePassingTestthis
package app04; class Point{ public int x; public int y; } public class ReferencePassingTest{ public static void increment(int x){ x++; } public static void reset(Point point){ point.x=0; point.y=0; } public static void main(String[] args){ int a=9; increment(a); System.out.println(a); Point p=new Point(); p.x=400; p.y=600; reset(p); System.out.println(p.x); } }
4.18 加載、鏈接和初始化
1.代碼清單4.13 StaticCodeTest命令行
package app04; public class StaticInitializationTest{ public static int a=5; public static int b=a*2; static{ System.out.println("static"); System.out.println(b); } public static void main (String[] args){ System.out.println("main method"); } }
4.19 對象建立初始化
1.代碼清單4.14 InitTest1類(實例初始化能夠訪問實例變量)
package app04; public class InitTest1{ int x=3; int y; { y=x*2; System.out.println(y); } static{ System.out.println("Static initialization"); } public static void main(String[] args){ InitTest1 test=new InitTest1(); InitTest1 moreTest=new InitTest1(); } }
2.代碼清單4.16 InitTest3類(將初始化代碼包裝到一個方法中)
package app04; public class InitTest3{ int x=3; int y; public InitTest3(){ init(); } public InitTest3(int x){ this.x=x; init(); } private void init(){ y=x*2; System.out.println(y); } static{ System.out.println("Static initialization"); } public static void main(String[] args){ InitTest3 test=new InitTest3(); InitTest3 moreTest=new InitTest3(); } }
學習總結——
第七章繼承
7.1 概覽
1.在Java中,一個類只可以擴展一個類。
2.代碼清單7.3 Animal類及其子類
package app07; class Animal{ public float weight; public void eat(){ } } class Bird extends Animal{ public int numberOfWings=2; public void fly(){ } } class Fish extends Animal{ public int numberOfFins=2; public void swim(){ } } class Dog extends Animal{ public int numberOflegs=4; public void walk(){ } }
7.3 方法覆蓋
1.代碼清單7.5 Box類(@Override一般用來表示被覆蓋的方法)
package app07; public class Box{ public int length; public int width; public int height; public Box(int length,int width,int height){ this.length=length; this.width=width; this.height=height; } @Override public String toString(){ return "I am a Box." } public Object clone(){ return new Box(1,1,1); } }
7.4 調用超類的構造方法
1.代碼清單7.6 調用超類的無參構造方法
package app07; class Base{ public Base(){ System.out.println(「Base」); } public Base(String s){ System.out.println("Base."+s); } } public class Sub extends Base{ public Sub(String s){ System.out.println(s); } public static void main(String[] args){ Sub sub=new Sub(「Start」); } }
類名對應子類的名稱。
7.5 調用超類的隱藏方法
1.代碼清單7.8 使用super訪問一個隱藏的成員
package app07; class Tool{ @Override public String toString(){ return "Generic tool"; } } public class Pencil extends Tool{ @Override public String toString(){ return"I am a Pencil"; } public void write(){ System.out.println(super.toString()); System.out.println(toString()); } public static void main(String[] args){ Pencil pencil=new Pencil(); pencil.write(); } }
第10章接口和抽象類
10.5 基類
1.TediousServlet類
package test; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class TediousServlet implements Servlet{ @Override public void init (ServletConfig config) throws ServletException{ } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException,IOException{ response.getWriter().print("Welcome"); } @Override public void destroy(){ } @Override public String getServletInfo(){ return null; } @Override public ServletConfig getServletConfig(){ return null; } }
10.6 抽象類
1.代碼清單10.6 DefaultPrinter的一個實現
package app10; public abstract class DefaultPrinter{ @Override public String toString(){ return "Use this to print documents."; } public abstract void print(Object document); } public class MyPrinter extends DefaultPrinter{ @Override public void print(Object document){ System.out.println("Printing document"); } }
第11章多態
1.多態是一種OOP特性,它容許一個對象根據接收到的一個方法調用來肯定要調用哪個方法實現。
2.代碼清單11.1 多態的一個示例
class Employee{ public void work(){ System.out.println("I am an employee."); } } class Manager extends Employee{ public void work(){ System.out.println("I am a manager.") } public void manage(){ System.out.println"Managing ..."); } } public class PolymorphismDemo1{ public static void main(String[] args) { Employee employee; employee=new Manager(); System.out.println(employee.getClass().getName()); employee.work(); Manager manager=(Manager) employee; manager.manage(); } }
package com.test; public class BaseClass { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Father f = new Father(); f.sayHi(); Son s = new Son(); s.sayHi(); s.sayHello(); Father fs = new Son(); fs.sayHi(); //Son sf = (Son) new Father(); //sf.aa(); //sf.sayHi(); System.out.println("---------------------------"); System.out.println(f.getHeight()); System.out.println(s.getHeight()); System.out.println(fs.getHeight()); System.out.println(fs.getClass()); } } class Father { public int height; Father(){ height = 5; } Father(int height){ this.height= height; } public void sayHi(){ System.out.println("Hi,World!I'm Father."); } public void aa(){ System.out.println("Hi,aa!I'm Father."); } public int getHeight(){ return height; } } class Son extends Father { public void sayHello(){ System.out.println("Hello,World!I'm Son."); } @Override public void sayHi(){ System.out.println("Hi,World!I'm Son."); } }
輸出爲:
Hi,World!I'm Father. Hi,World!I'm Son. Hello,World!I'm Son. Hi,World!I'm Son. --------------------------- 5 5 5 class com.test.Son
總結以下:
1.父類引用指向父類對象,子類引用指向子類對象,就是正常的類生成。
2.父類引用指向子類對象時,父類引用能夠調用父類裏定義的方法,好比sayHi();可是不能調用父類沒用,子類有的方法,好比sayHello();會報The method sayHello() is undefined for the type Father錯誤。可是,父類引用指向子類對象時,調用的方法是子類的,也就是控制檯輸出的「Hi,World!I'm Son.」調用.getClass(),會打印"class com.test.Son"。
3.因爲Son繼承Father,因此全部的.getHeight();都是輸出5.
4.子類對象指向父類引用的,須要強制轉換成子類,見代碼的註釋地方,既能夠調用父類方法,也能夠調用子類方法,可是會報com.test.Father cannot be cast to com.test.Son異常。
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一週 | 200/200 | 2/2 | 20/20 | |
第二週 | 300/500 | 2/3 | 18/38 |