6.1
继承基本概念
只允许多层继承,不允许多重继承,不能直接访问父类中的私有成员,子类可以调用父类中非私有方法
package org.lxh.demo;
class Person{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Students extends Person{
}
public class TestJava{
public static void main(String[] args) {
Students stu = new Students();
stu.setName("张三");
stu.setAge(30);
System.out.println("姓名:" + stu.getName() + ",年龄:" + stu.getAge());
}
}
6.2
子类对象实例化过程
子类对象实例化之前必须首先调用父类中的构造方法再调用子类自己的构造方法
class Person{
private String name;
private int age;
public Person() {
System.out.println("父类Person中的构造");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Students extends Person{
private String school;
public Students() {
super();//加不加此句效果相同,子类直接使用super()调用父类中的无参构造
System.out.println("子类中的构造");
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
public class TestJava{
public static void main(String[] args) {
Students stu = new Students();
stu.setName("张三");
stu.setAge(30);
stu.setSchool("清华大学");
System.out.println("姓名:" + stu.getName() + ",年龄:" + stu.getAge() + ",学校:" +stu.getSchool());
}
}
6.3方法的覆写
子类中定义了与父类中同名的方法,但覆写时必须考虑权限,即被子类覆写的方法不能拥有比父类方法更加阉割的访问权限
private<default<public
覆写后子类对象调用的是被覆写后的方法,若要调用父类中的方法,可用super
从父类的private不能覆写到子类的default
package org.lxh.demo;
class Person{
void print() {
System.out.println("Person --> void print(){}");
}
}
class Students extends Person{
public void print() {
System.out.println("Student --> void print()");
}
}
public class TestJava{
public static void main(String[] args) {
new Students().print();
}
}
package org.lxh.demo;
class Person{
void print() {
System.out.println("Person->print");
}
}
class Student extends Person{
public void print() {
super.print();
System.out.print("Student->print");
}
}
public class TestJava {
public static void main(String args[]) {
new Student().print();
}
}
属性的覆写,就近确认属性
package org.lxh.demo;
class Person{
public String info = "MLDN";
void print() {
System.out.println("Person->print");
}
}
class Student extends Person{
public String info = "LXH";
public void print() {
System.out.println(super.info);
System.out.print(this.info);
}
}
public class TestJava {
public static void main(String args[]) {
new Student().print();
}
}
6.2.3
super 的作用
调用父类构造方法
若子类中无相应属性,则this从父类中查找,super与this不可同时出现
package org.lxh.demo;
class Person{
private String name;
private int age;
public Person(String name, int age) {
this.age = age;
this.name = name;
}
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return "姓名:"+this.getName()+";年龄:"+this.age;
}
}
class Student extends Person{
private String school;
public Student(String name,int age,String school) {
super(name,age);
setSchool(school);
}
public String getSchool() {
return this.school;
}
public void setSchool(String school) {
this.school = school;
}
public String getInfo() {
return super.getInfo()+";学校:" + this.getSchool();
}
}
public class TestJava {
public static void main(String args[]) {
Student stu = new Student("张三", 30, "清华大学");
System.out.println(stu.getInfo());
}
}
6.4
final 关键字
完结器,可声明属性,类,方法
声明的类不能有子类,声明的方法不能被子类所覆写,声明变量即为常量,不可修改
6.5抽象类的基本概念
专门作为父类,类似模板,要求设计者根据他的格式来创建新类,但不能直接由抽象类创建对象。
包含一个抽象方法的类必须是抽象类,抽象类与抽象方法都要使用abstract关键字声明,抽象方法只需声明,不需实现,抽象类必须被子类继承,子类(如果不是抽象类)必须覆写抽象类中全部的抽象方法。抽象方法不能用private 修饰,子类此时无法覆写,也不要用final修饰,不能被外部直接调用
package org.lxh.demo;
abstract class A{
public static final String FLAG = "CHINA";
private String name = "李兴华";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public abstract void print();
}
class B extends A{
public void print() {
System.out.println("FLAG="+FLAG);
System.out.println("姓名="+super.getName());
}
}
public class TestJava{
public static void main(String args[]) {
B b = new B();
b.print();
}
}
6.6接口的基本概念
特殊的类,由全局常量与公共的抽象方法组成,抽象方法必须定义为public,如果不写则默认为Public
同抽象类,子类通过implements关键字实现接口,才可使用,可同时实现多个接口,子类可同时继承抽象类与接口,一个抽象类实现多个接口,但接口不允许继承抽象类,可以继承多个接口
package org.lxh.demo;
interface A {
public String AUTHOR = "李兴华";
public void print();
public String getInfo();
}
interface B {
public void say();
}
class X implements A, B {
public void say() {
System.out.println("Hello World!!!");
}
public String getInfo() {
return "HELLO";
}
public void print() {
System.out.println("作者:" + AUTHOR);
}
}
public class TestJava {
public static void main(String args[]) {
X x = new X();
x.say();
x.print();
}
}
package org.lxh.demo;
interface A {
public String AUTHOR = "李兴华";
public void print();
public String getInfo();
}
abstract class B implements A{
public abstract void say();
}
class X extends B{
public void say() {
System.out.println("Hello World!!!");
}
public String getInfo() {
return "HELLO";
}
public void print() {
System.out.println("作者:" + AUTHOR);
}
}
public class TestJava {
public static void main(String args[]) {
X x = new X();
x.say();
x.print();
}
}
package org.lxh.demo;
interface A {
public String AUTHOR = "李兴华";
public void printA();
}
interface B {
public void printB();
}
interface C extends A, B {
public void printC();
}
class X implements C {
public void printA() {
System.out.println("A、Hello World");
}
public void printB() {
System.out.println("B.Hello World");
}
public void printC() {
System.out.println("C");
}
}
public class TestJava {
public static void main(String args[]) {
X x = new X();
x.printA();
x.printB();
x.printC();
}
}
6.7 对象的多态性
(1)向上转型:子类对象->父类对象(自动)
(2)向下转型:父类对象->子类对象(必须指名要转型的子类类型)
此时父类对象调用的方法为子类覆写过的方法,但不能调用子类的方法,若想调用,可进行向下转型。(必须首先发生向上转型,再发生向下转型,否则异常)
此时,fun2()调用的方法是被子类覆写过的方法。
package org.lxh.demo;
class A {
public void fun1() {
System.out.println("A-->public void fun1()");
}
public void fun2() {
this.fun1();
}
}
class B extends A {
public void fun1() {
System.out.println("B-->public void fun1()");
}
public void fun3() {
System.out.println("B-->public void fun3()");
}
}
public class TestJava {
public static void main(String args[]) {
A a = new B();
B b = (B) a;
a.fun1();
b.fun1();
b.fun2();
b.fun3();
}
}
package org.lxh.demo;
import org.junit.validator.PublicClassValidator;
class A {
public void fun1() {
System.out.println("A-->public void fun1()");
}
public void fun2() {
this.fun1();
}
}
class B extends A {
public void fun1() {
System.out.println("B-->public void fun1()");
}
public void fun3() {
System.out.println("B-->public void fun3()");
}
}
class C extends A {
public void fun1() {
System.out.println("C-->public void fun1()");
}
public void fun5() {
System.out.println("C-->public void fun5()");
}
}
public class TestJava {
public static void main(String args[]) {
fun(new B());
fun(new C());
}
public static void fun(A a) {
a.fun1();
}
}
6.8 instanceof关键字
使用instanceof关键字判断对象到底是那个类的实例
package org.lxh.demo;
class A {
public void fun1() {
System.out.println("A-->public void fun1()");
}
public void fun2() {
this.fun1();
}
}
class B extends A {
public void fun1() {
System.out.println("B-->public void fun1()");
}
public void fun3() {
System.out.println("B-->public void fun3()");
}
}
public class TestJava {
public static void main(String args[]) {
A a1 = new B();
System.out.println("A a1 = new B()" + (a1 instanceof A));
System.out.println("A a1 = new B()" + (a1 instanceof B));
A a2 = new A();
System.out.println("A a2 = new A()" + (a2 instanceof A));
System.out.println("A a2 = new A()" + (a2 instanceof B));
}
}
子类实例化对象同时是子类与父类的实例,故可以在向下转型前进行判断
package org.lxh.demo;
class A {
public void fun1() {
System.out.println("A-->public void fun1()");
}
public void fun2() {
this.fun1();
}
}
class B extends A {
public void fun1() {
System.out.println("B-->public void fun1()");
}
public void fun3() {
System.out.println("B-->public void fun3()");
}
}
class C extends A {
public void fun1() {
System.out.println("C-->public void fun1()");
}
public void fun5() {
System.out.println("C-->public void fun5()");
}
}
public class TestJava {
public static void main(String args[]) {
fun(new B());
fun(new C());
}
public static void fun(A a) {
a.fun1();
if(a instanceof B) {
B b = (B)a;
b.fun3();
}
if(a instanceof C) {
C c = (C)a;
c.fun5();
}
}
}
使用多态性对抽象类或接口实例化
package org.lxh.demo;
abstract class A {
public abstract void print();
}
class B extends A {
public void print() {
System.out.println("Hello World!!!");
}
}
public class TestJava {
public static void main(String args[]) {
A a = new B();
a.print();
}
}
模板设计
package org.lxh.demo;
abstract class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public void say() {
System.out.println(this.getContent());
}
public abstract String getContent();
}
class Student extends Person {
private float score;
public Student(String name, int age, float score) {
super(name, age);
this.score = score;
}
public String getContent() {
return "name" + super.getName() + "age" + super.getAge() + "score" + this.score;
}
}
class Worker extends Person {
private float salary;
public Worker(String name, int age, float salary) {
super(name, age);
this.salary = salary;
}
public String getContent() {
return "name" + super.getName() + super.getAge() + salary;
}
}
public class TestJava {
public static void main(String args[]) {
Person per1 = null;
Person per2 = null;
per1 = new Student("bnuij", 20, 78.4f);
per2 = new Worker("hji", 30, 30000.0f);
per1.say();
per2.say();
}
}
标准设计
package org.lxh.debugdemo;
interface USB{
public void start();
public void stop();
}
class Flash implements USB{
public void start() {
System.out.println("U盘开始工作");
}
public void stop() {
System.out.println("U盘停止工作");
}
}
class Print implements USB{
public void start() {
System.out.println("打印机开始工作");
}
public void stop() {
System.out.println("打印机结束工作");
}
}
class Computer{
public static void plugin(USB usb) {
usb.start();
System.out.println("USB设备工作");
usb.stop();
}
}
public class DebugDemo{
public static void main(String args[]) {
Computer.plugin(new Flash());
Computer.plugin(new Print());
}
}
工厂模式
使用className.equals("apple")可能出现空指向异常
package org.lxh.debugdemo;
interface Fruit {
public void eat();
}
class Apple implements Fruit {
public void eat() {
System.out.println("**吃苹果");
}
}
class Orange implements Fruit {
public void eat() {
System.out.println("**吃橘子");
}
}
class Factory {
public static Fruit getInstance(String className) {
Fruit f = null;
if (className.equals("apple")) {
f = new Apple();
}
if (className.equals("orange")) {
f = new Orange();
}
return f;
}
}
public class DebugDemo {
public static void main(String args[]) {
Fruit f = null;
f = Factory.getInstance("apple");
f.eat();
}
}
适配器模式,图形界面编程常用
package org.lxh.debugdemo;
interface Window {
public void open();
public void close();
public void activated();
public void iconified();
public void deiconified();
}
abstract class WindowAdapter implements Window {
public void open() {
};
public void close() {
};
public void iconified() {
};
public void deiconified() {
};
public void activated() {
};
}
class WindowImpl extends WindowAdapter {
public void open() {
System.out.println("窗口打开");
}
public void close() {
System.out.println("窗口关闭");
}
}
public class DebugDemo {
public static void main(String args[]) {
Window win = new WindowImpl();
win.open();
win.close();
}
}
若有可用的抽象类和接口最好优先使用接口
内部类的扩展,一个抽象类中可以定义多个抽象类或接口,一个接口中可以定义多个抽象类或接口
6.11
在Java中只要一个类没有显式继承一个类,则必定为Object子类
public Object()构造方法
public boolean equals(Object obj)对象比较·
public String toString()对象打印时调用
public int hashCode()取得hash码
最好覆写equals,hasCode,toString.
tostring()对象输出时必定会调用
package org.lxh.debugdemo;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.age = age;
this.name = name;
}
public String toString() {
return "naem:" + name + " " + "age:" + age;
}
}
public class DebugDemo {
public static void main(String args[]) {
Person per = new Person("李兴华", 30);
System.out.println("对象信息:" + per);
}
}
equals()默认比较地址信息
package org.lxh.debugdemo;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.age = age;
this.name = name;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Person)) {
return false;
}
Person per = (Person) obj;
if (per.name.equals(this.name) && per.age == this.age) {
return true;
} else {
return false;
}
}
public String toString() {
return "name:" + name + " " + "age:" + age;
}
}
public class DebugDemo {
public static void main(String args[]) {
Person per1 = new Person("李兴华", 30);
Person per2 = new Person("李兴华", 30);
System.out.println(per1.equals(per2) ? "是同一个人" : "不是同一个人");
System.out.println(per1.equals("hello") ? "是" : "不是");
}
}
接收引用类型的对象
package org.lxh.debugdemo;
public class DebugDemo {
public static void main(String args[]) {
int temp[] = { 1, 3, 5, 7, 9 };
print(temp);
}
public static void print(Object o) {
if (o instanceof int[]) {
int x[] = (int[]) o;
for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + "\t");
}
}
}
}
6.12包装类
将基本数据类型进行包装,使之成为对象
Integer,Byte,Float,Double,Short,Long都属于Number的子类
package org.lxh.debugdemo;
public class DebugDemo {
public static void main(String args[]) {
int x = 30;
Integer i = new Integer(x);//装箱
int temp = i.intValue();//拆箱
Integer ji = 30;//自动装箱
int temp9 = ji;//自动拆箱
}
}
包装类的应用
package org.lxh.debugdemo;
public class DebugDemo {
public static void main(String args[]) {
String str1 = "30";
String str2 = "30.3";
int x = Integer.parseInt(str1);
float f = Float.parseFloat(str2);
System.out.println(x+x);
System.out.println(f+f);
}
}
6.13匿名内部类
package org.lxh.debugdemo;
public class DebugDemo {
public static void main(String args[]) {
new X().fun1();
}
}
interface A {
public void printInfo();
}
class X {
public void fun1() {
this.fun2(new A() {
public void printInfo() {
System.out.println("Hello World!!!");
}
});
}
public void fun2(A a) {
a.printInfo();
}
}
网友评论