发布于 2017-11-13 00:28:11 | 155 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java并发编程示例,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


子类使用extends继承父类是Java面向对象编程中的基础知识,这里我们就来详解Java中使用externds关键字继承类的用法,需要的朋友可以参考下

理解继承是理解面向对象程序设计的关键。在Java中,通过关键字extends继承一个已有的类,被继承的类称为父类(超类,基类),新的类称为子类(派生类)。在Java中不允许多继承。
(1)继承


class Animal{ 
  void eat(){ 
    System.out.println("Animal eat"); 
  } 
  void sleep(){ 
    System.out.println("Animal sleep"); 
  } 
  void breathe(){ 
    System.out.println("Animal breathe"); 
  } 
} 
 
class Fish extends Animal{ 
} 
 
public class TestNew { 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Animal an = new Animal(); 
    Fish fn = new Fish(); 
     
    an.breathe(); 
    fn.breathe(); 
  } 
} 

在eclipse执行得:


Animal breathe! 
Animal breathe! 

.java文件中的每个类都会在文件夹bin下生成一个对应的.class文件。执行结果说明派生类继承了父类的所有方法。

(2)覆盖


class Animal{ 
  void eat(){ 
    System.out.println("Animal eat"); 
  } 
  void sleep(){ 
    System.out.println("Animal sleep"); 
  } 
  void breathe(){ 
    System.out.println("Animal breathe"); 
  } 
} 
 
class Fish extends Animal{ 
  void breathe(){ 
    System.out.println("Fish breathe"); 
  } 
} 
 
public class TestNew { 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Animal an = new Animal(); 
    Fish fn = new Fish(); 
     
    an.breathe(); 
    fn.breathe(); 
  } 
} 

执行结果:


Animal breathe
Fish breathe

在子类中定义一个与父类同名,返回类型,参数类型均相同的一个方法,称为方法的覆盖。方法的覆盖发生在子类与父类之间。另外,可用super提供对父类的访问。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务