面向对象基础十

来源:互联网 发布:联通拨号软件 编辑:程序博客网 时间:2024/05/16 07:01

 

对象多态性:
开发建议:不要去继承一个已经实现好的类-------接口与抽象类
 
 
abstract class Person
{
       public void say()
       {
              System.out.println(this.getContent());
       }
       public abstract String getContent();
}
 
class Worker extends Person
{
       public String getContent()
       {
              return "工人说话了。。。";
       }
}
 
class Student extends Person
{
       public String getContent()
       {
              return "学生说话了。。。";
       }
}
 
public class javaoo05
{
       public static void main(String []args)
       {
              Person p = new Student();
              p.say();
       }
}
如果以后在开发中需要继承,则80%情况下继承的都会是一个抽象类,当然,也有一个问题,抽象类有java单继承局限,不能多继承
 
abstract class Person
{
       private String name;
       private int age;
       public Person(String name,int age)
       {
              this.setName(name);
              this.setAge(age);
       }
       public void setName(String name)
       {
              this.name = name;
       }
       public void setAge(int age)
       {
              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 Worker extends Person
{
       private float salary;
       public Worker(String name,int age,float salary)
       {
              super (name,age);
              this.setSalary(salary);
       }
       public void setSalary(float salary)
       {
              this.salary = salary;
       }
       public float getSalary()
       {
              return this.salary;
       }
       public String getContent()
       {
              return "工人说----姓名:" + super.getName() + ",年龄:" + getAge() + ",工资:" + this.getSalary();
       }
}
 
class Student extends Person
{
       private float score;
       public Student (String name, int age, float score)
       {
              super(name,age);
              this.setScore(score);
       }
       public void setScore(float score)
       {
              this.score = score;
       }
       public float getScore()
       {
              return this.score;
       }
       public String getContent()
       {
              return "学生说----姓名:" + super.getName() + ",年龄:" + super.getAge() + ",分数:" + this.getScore();
       }
}
 
public class javaoo05
{
       public static void main(String []args)
       {
              Person p = null;
              p = new Student("张三",30,90);
              // p = new Worker("张三",30,3000);
              p.say();
       }
}
 
 
 
interface A
{
       public void fun();
}
 
class B implements A
{
       public void fun()
       {
              System.out.println("Hello World!!!");
       }
}
 
public class javaoo05
{
       public static void main(String []args)
       {
              A a = new B();
              a.fun();
       }
}
 
################################################################
 
 
interface USB
{
       public void start();
       public void stop();
}
 
class PC
{
       public static void plugin(USB u)
       {
              u.start();
              u.stop();
       }
}
 
class Mp3 implements USB
{
       public void start()
       {
              System.out.println("Mp3开始工作了。。。" );
       }
       public void stop()
       {
              System.out.println("Mp3停止工作了。。。");
       }
}
 
class UDisk implements USB
{
       public void start()
       {
              System.out.println("U盘开始工作了。。。");
       }
       public void stop()
       {
              System.out.println("U盘停止工作了。。。");
       }
}
 
public class javaoo05
{
       public static void main(String []args)
       {
              PC.plugin(new UDisk());
              PC.plugin(new Mp3());
       }
}
 
U盘开始工作了。。。
U盘停止工作了。。。
Mp3开始工作了。。。
Mp3停止工作了。。。
 
 
 
interface A
{
       public void fun1();
       public void fun2();
       public void fun3();
}
 
abstract class B implements A
{
       public void fun1()
       {}
       public void fun2()
       {}
       public void fun3()
       {}
}
 
class C extends B
{
       public void fun1()
       {
              System.out.println("Hello World!!");
       }
}
 
 
public class javaoo05
{
       public static void main(String []args)
       {
              A a = new C();
              //a.fun2();
              a.fun1();
       }
}
 
 
 
接口的使用:   将方法名称暴露给用户
接口对象的实例: 通过对象的多态性,由子类为接口对象实例化
接口的实际应用:USB(接口)----插入之后开始工作,工作完成之后停止退出
                            PC------àUSB <-------MP3.UDISK
 
抽象类与接口在使用上如此相似,那么该选谁?
开发上讲:优先使用接口,接口允许多继承
 
 
interface USB
{
       public void start();
       public void stop();
}
 
class Mp3 implements USB
{
       public void start()
       {
              System.out.println("Mp3开始工作了。。。");
       }
       public void stop()
       {
              System.out.println("Mp3停止工作了。。。");
       }
}
 
class UDisk implements USB
{
       public void start()
       {
              System.out.println("U盘开始工作了。。。");
       }
       public void stop()
       {
              System.out.println("U盘结束工作了。。。");
       }
}
 
class Factory
{
       public static USB getUSBInstance()
       {
              //return new Mp3();
              return new UDisk();
       }
}
 
public class javaoo05
{
       public static void main(String []args)
       {
              USB u = Factory.getUSBInstance();
              u.start();
              u.stop();
       }
 
}
 
接口与抽象类:

 
抽象类
接口
相同
对象不能直接实例化,通过多态性,可由其子类实例化
不同
包括一般方法、抽象方法、变量、常量
包括常量、抽象方法
可以有构造方法
不能有构造方法
抽象类可以实现多个接口
接口不能继承一个抽象类
继承时,单继承会有局限
解决单继承带来的局限

 
原创粉丝点击