您的位置:首页 > 编程语言 > Java开发

Java 入门 之 面向对象 封装、继承与多态(二)

2016-01-27 17:29 751 查看
继承

class 子类 extends 父类【c/c++之中,class B:public A 】

继承的限制

Java只允许单继承

子类不能访问父类的私有成员(这个使用父类的public或者default方法就可解决)

方法的重写与重载

范围:重载只存在于一个类中,重写则出现在继承的类中。

定义:重载为同名同返回值不同参数,重写要求三者都相同。

权限:重写方法要比父类中的方法范围小。

private < default < public

代码示例

package Rewrite_Pack;

class Father{
	public void tell(){
		System.out.println("这里是父类!!!");
	}	
}
class Son extends Father{
	public void tell(){
		System.out.println("这里是子类!!!");
	}
}

public class Rewrite_Class {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Son sonobj = new Son();
		sonobj.tell();
	}

}


抽象类与接口

final关键字,又称为完结器,它可以声明类、函数以及变量。

它修饰的类不能够被继承,函数不可被重写,变量不可被修改。

抽象类

包含抽象方法的类称为抽象类。

用 abstract 关键字声明的方法称为抽象方法。

抽象类被子类继承后,必须重写抽象类中的抽象方法。

例子:

abstract class Myclass

{

抽象方法

}

抽象类不能够实例化,只能实例化其子类,即,不可以new抽象类。

接口

接口是一种特别的类,它只能由全局变量和公共抽象方法组成;

例子

interface ClassName{

全局变量;// public static final

抽象方法;

}

接口的实现必须通过关键字implements连接子类,在子类中实现相应功能。接口可以实现多继承,父类之间采用逗号隔开。

一个类可以继承多个接口和多个抽象类。

class A extends Abs1,Abs2 impements Imp1,Imp2 { }

一个接口不能继承一个抽象类,但可以使用extends关键字继承多个接口,实现接口多继承。

多态性

多态性分为方法的重载与重写,对象的多态性。

对象的多态性分为

向上转型:程序自动完成。

形式:

父类 父类对象 = 子类实例;

向下转型:强制类型转换。

形式:

子类 子类对象 = (子类)父类对象实例

package ABS_Pack;

import java.security.PublicKey;

class A{
	public void tell01()
	{
		System.out.println("this is the tell 01 A");
	}	
	public void tell02()
	{
		System.out.println("this is the tell 02 A");
	}	
}
class B extends A{
	public void tell01(){
		System.out.println("this is the tell 01 B");
	}
	public void tell03(){
		System.out.println("this is the tell 03 A");
	}
}

public class ABS_Class {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 向上转型 :父类 父类对象 = 子类实例;
		B b = new B();
		
		System.out.println(b instanceof A);
		System.out.println(b instanceof B);
		
		A a = b;	

		System.out.println(a instanceof A);
		System.out.println(a instanceof B);
		
		a.tell01(); 
		a.tell02();
		// a.tell03();
		// 因为 tell01 是重写的,所以 调用是被重写的方法
		// 向下转型
		// 向下转型时必须先发生向上转型,才能发生向下转型
		A aa = new B();
		System.out.println(aa instanceof A);
		System.out.println(aa instanceof B);
		B bb = (B)aa;
		System.out.println(bb instanceof A);
		System.out.println(bb instanceof B);
		bb.tell01();
		bb.tell02();
		bb.tell03();
	}

}
代码输出:

true
true
true
true
this is the tell 01 B
this is the tell 02 A
true
true
true
true
this is the tell 01 B
this is the tell 02 A
this is the tell 03 A
注:

instanceof 实现的是对象和类的判断。

抽象类和接口的应用

1.抽象类的应用

父类有自定义构造方法,子类一定要复写;

习惯上不要去继承完整被定义的类。

2.接口的应用代码示例

package Inter_App;

// 实现 打印机和优盘对 USB 接口的使用
interface USB{
	void start();
	void stop();
}

class contrler{
	public static void work(USB u) {
		u.start();
		System.out.println("工作中");
		u.stop();
	}
}

class UDisk implements USB{
	public void start(){
		System.out.println("U 盘启动");
	}
	public void stop(){
		System.out.println("U 盘停止");
	}
}

class Printer implements USB{
	public void start(){
		System.out.println("打印机 启动");
	}
	public void stop(){
		System.out.println("打印机 停止");
	}
}

public class Interface_Application {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 鉴于代码只启用一次,故输出使用匿名函数
		contrler.work(new UDisk());
		contrler.work(new Printer());		
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: