抽象类实例化方法:
package org.abstractdemo;
abstract class A{
public abstract void fun();
}
class B extends A{
public void fun(){
System.out.println("Hello");
}
}
public class abstractdemo01 {
public static void main(String args[]){
A a = new B();
a.fun();
}
}
抽象类功能 实现模版:
package org.abstractdemo;
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(){
this.getContent();
}
public abstract void getContent();
}
class Student extends Person{
private float score;
public Student(String name,int age,float score){
super(name,age);
this.score = score;
}
public float getScore(){
return this.score;
}
public void getContent(){
System.out.println("学生说:--> 姓名:"+super.getName()+"年龄:"+super.getAge()+"成绩:"+this.getScore());
}
}
class Worker extends Person{
private float salare;
public Worker(String name,int age,float salare){
super(name,age);
this.salare = salare;
}
public float getSalare(){
return this.salare;
}
public void getContent(){
System.out.println("工人说:--> 姓名:"+super.getName()+"年龄:"+super.getAge()+"成绩:"+this.getSalare());
}
}
public class abstractdemo02 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p1 = new Student("张三",20,89.0f);
p1.Say();
Person p2 = new Worker("李四",20,1189.0f);
p2.Say();
}
}
接口类实例化方法:
package org.interfacedemo;
interface A{
public void fun();
}
class B implements A{
public void fun(){
System.out.println("Hello");
}
}
public class InterfaceDemo01 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A a = new B();
a.fun();
}
}
接口类功能实现标准:
package org.interfacedemo;
interface Usb{
public void start();
public void stop();
}
class Computer{
public static void plugs(Usb usb){
usb.start();
usb.stop();
}
}
class Flash 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 InterfaceDemo02 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Computer.plugs(new Flash());
Computer.plugs(new Printer());
}
}
多接口实例:
package org.interfacedemo;
interface A{
public static final String INFO="hello";
public void fun();
}
interface C{
public void func();
}
class B implements A,C{
public void fun(){
System.out.println(INFO);
}
public void func(){
System.out.println(INFO);
}
}
public class InterfaceDemo01 {
public static void main(String args[]){
B b = new B();
b.fun();
b.func();
}
}
实例化抽象类及接口:
package org.interfacedemo;
interface A{
public void funa();
}
interface B{
public void funb();
}
abstract class C{
public abstract void func();
}
class D extends C implements A,B{
public void funa(){};
public void funb(){};
public void func(){};
}
public class InterfaceDemo02 {
public static void main(String args[]){
D d = new D();
d.funa();
d.funb();
d.func();
}
}
适配器设计模式:
package org.interfacedemo;
interface Window{
public void open();
public void close();
public void icon();
public void unicon();
}
abstract class WindowAdpater implements Window{
public void open(){}
public void close(){}
public void icon(){}
public void unicon(){}
}
class MyWindow extends WindowAdpater{
public void open(){
System.out.println("打开窗口");
}
}
public class InterfaceDemo03 {
public static void main(String args[]){
MyWindow mw = new MyWindow();
mw.open();
}
}
工厂类设计模式:
package org.interfacedemo;
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 ("apple".equals(classname)){
f = new Apple();
}
if ("orange".equals(classname)){
f = new Orange();
}
return f;
}
}
public class InterfaceDemo04 {
public static void main(String args[]){
Fruit f = Factory.getInstance("orange");
if (f!=null){
f.eat();
}
}
}
代理设计模式:
package org.interfacedemo;
interface Give{
public void giveMoney();
}
class GiveReal implements Give{
public void giveMoney(){
System.out.println("Content");
}
}
class ProxeyGive implements Give{
private Give g = null;
public ProxeyGive(Give g){
this.g = g;
}
public void before(){
System.out.println("before to do");
}
public void giveMoney(){
this.before();
this.g.giveMoney();
this.after();
}
public void after(){
System.out.println("after to do");
}
}
public class InterfaceDemo05 {
public static void main(String args[]){
Give g = new ProxeyGive(new GiveReal());
g.giveMoney();
}
}
抽象类与接口区别:
NO | 比较点 | 抽象类 | 接口 |
1 | 组成 | 抽象方法、普通方法、常量、变量、构造方法、全局常量 | 抽象方法、全局全量 |
2 | 定义 | abstract | interface |
3 | 子类 | 子类通过extends 继承抽象类 | 子类通过implements实现接口 |
4 | 限制 | 一个子类只能继承一个抽象类 | 一个子类可以同时实现多个接口 |
5 | 关系 | 一个抽象类可以实现多个接口 | 一个接口不能继承一个抽象类只能实现多个接口 |
一个抽象类中可以包含多个接口 | 一个接口中可以包含多个抽象类 | ||
6 | 设计模式 | 模板设计 | 工厂设计、代理设计 |
共同 | 适配器设计 | ||
7 | 实例化 | 都是通过对象的多态性,通过子类进行对象的实例化操作 | |
8 | 实现限制 | 存在单继承局限 | 不存在此类局限 |
9 | 特性 | 表示一个标准、一种能力 |