在JAVA中有一个特型叫继承(Inheritance),通过继承我们可以重复使用代码,令代码简洁,易于扩展。例如:有一个sharp的类,这个类实现了sharp的一些方法,现在我们要写一个circle的类,我们想了想,呀circle属于sharp的一种呀,我们可以继承sharp呀!对,这就是继承的奥妙!

请看下面的代码:

/**
* @author gavin
* 这是一个描述形状的类
*/
class Sharp {
public double area(){
return 0;
} /*
* 或得面积较大的Sharp
*/
static Sharp getLarger(Sharp a,Sharp b){
if (a.area()>b.area())
return a;
else
return b;
} } /**
*
* @author gavin
* 这是一个Circle类,继承Sharp
*/
class Circle extends Sharp{ private static final double PI = Math.PI;
private double radius=0; Circle(int radius){
this.radius =radius;
} public double area(){ //此方法覆盖了父类Sharp中的同名方法
return PI*this.radius*this.radius;
} //注意,Circle 继承 Sharp,所以Circle拥有Sharp中的getLarger方法
}
/**
*
* @author gavin
* 这是一个Square类,继承Sharp
*/
class Square extends Sharp{
private double side = 0; Square(double side){
this.side = side;
}
public double area(){ //此方法覆盖了父类Sharp中的同名方法
return side*side;
}
//注意,Square 继承 Sharp,所以Square拥有Sharp中的getLarger方法 public static void main(String[] args){
Sharp a = new Circle(2);//一个父类可以有多个子类,如这里Sharp就有两个孩子,他们分别是Circle和Square。这里父类Sharp可以接受其所有子类的对象。
Sharp b= new Square(2);
Sharp larger = Sharp.getLarger(a, b);//父类Sharp不考虑它的子类是什么形状,会自动调用起子类对象的方法,因为方法被继承了。
System.out.println("面积较的形状的面积是:" + larger.area()); } }

007 The Inheritance In JAVA的更多相关文章

  1. Summary: Java Inheritance

    In this tutorial we will discuss about the inheritance in Java. The most fundamental element of Java ...

  2. 【译】Core Java Questions and Answers【1-33】

    前言 译文链接:http://www.journaldev.com/2366/core-java-interview-questions-and-answers Java 8有哪些重要的特性 Java ...

  3. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  4. Thinking in Java——笔记(9)

    Polymorphism Abstract classes and methods If you have an abstract class, objects of that specific cl ...

  5. The main difference between Java & C++(转载)

    转载自:http://stackoverflow.com/questions/9192309/the-main-difference-between-java-c C++ supports point ...

  6. Java Main Differences between Java and C++

    转载自:http://www.cnblogs.com/springfor/p/4036739.html C++ supports pointers whereas Java does not. But ...

  7. Core Java Volume I — 1.2. The Java "White Paper" Buzzwords

    1.2. The Java "White Paper" BuzzwordsThe authors of Java have written an influential White ...

  8. Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses

    5.1. Classes, Superclasses, and SubclassesLet's return to the Employee class that we discussed in th ...

  9. Lambdas in Java 8--reference

    Part 1 reference:http://jaxenter.com/lambdas-in-java-8-part-1-49700.html Get to know lambda expressi ...

随机推荐

  1. Object-C中需要注意的小细节

    --------------------------------------------关于命名------------------------------------------------- 1. ...

  2. 替换、恢复Html中的特殊字符

    public static string HtmlEncode(string theString){theString = theString.Replace(">", &q ...

  3. 【VB技巧】VB ListView 控件功能使用详解

    来源:http://lcx.cc/?i=494 ListView控件 在工具箱上击鼠标右键,选择快捷菜单的Components(部件)项,在控件列表中选择Microsoft Windows Commo ...

  4. hi35183e增加exfat文件系统的支持

    64G-128G的tf卡文件系统格式为exfat,而hi3518e默认只支持fat32格式的tf卡.为了挂载64G以上的tf卡,只能将sd卡先格式化成FAT32.鉴于exfat性能比FAT32强,因此 ...

  5. Spark配置参数调优

    1.配置多个executor 在项目中,由于数据量为几百万甚至千万级别,如果一个executor装载的对象过多,会导致GC很慢.项目中,我们使一个worker节点执行app时启动多个executor, ...

  6. PO_PO系列 - 安全文件管控管理分析(案例)

    2014-07-01 Created By BaoXinjian

  7. python(14)类,方法,对象,实例

    类: 简单来说就是一个函数的集合,在这个集合里面你定义了很多个函数: 方法:其实就是你定义的这些函数. 对象:简单来说就是自身具有多个属性(也可以说是子变量)的变量而已. 在下面的例子中class P ...

  8. db2删除数据库

    1.断开所有连接  db2 stop application all   force 2.停止数据库    db2stop 3.删除数据库(系统管理员权限下)db2 drop  数据库name

  9. git撤销命令

    1 撤销工作区中master的修改 git checkout -- master.txt ,用暂存去覆盖工作区 2 git clean -n 查看哪些文件会被移除3 git clean -f 强制删除 ...

  10. angularJs编写多指令的情况

    本实例主要展示controller和link参数的使用.以及多个指令同时作用的情况. <!DOCTYPE html> <html ng-app="myModule" ...