目录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 |