007 The Inheritance In JAVA
在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的更多相关文章
- Summary: Java Inheritance
In this tutorial we will discuss about the inheritance in Java. The most fundamental element of Java ...
- 【译】Core Java Questions and Answers【1-33】
前言 译文链接:http://www.journaldev.com/2366/core-java-interview-questions-and-answers Java 8有哪些重要的特性 Java ...
- 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 ...
- Thinking in Java——笔记(9)
Polymorphism Abstract classes and methods If you have an abstract class, objects of that specific cl ...
- The main difference between Java & C++(转载)
转载自:http://stackoverflow.com/questions/9192309/the-main-difference-between-java-c C++ supports point ...
- Java Main Differences between Java and C++
转载自:http://www.cnblogs.com/springfor/p/4036739.html C++ supports pointers whereas Java does not. But ...
- 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 ...
- 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 ...
- Lambdas in Java 8--reference
Part 1 reference:http://jaxenter.com/lambdas-in-java-8-part-1-49700.html Get to know lambda expressi ...
随机推荐
- [转]利于ThreadLocal管理Hibernate Session
摘自http://aladdin.iteye.com/blog/40986 在利用Hibernate开发DAO模块时,我们和Session打的交道最多,所以如何合理的管理Session,避免Sessi ...
- 【linux】Cache和Buffer的区别
- LintCode "Post Office Problem" !!!
* Non-intuitive state design class Solution { public: /** * @param A an integer array * @param k an ...
- 【LVM】LVM自动扩容脚本
概要说明: /dev/mapper/vg0-data挂载在目录/data下: 当新增物理磁盘时,需要自动对/dev/mapper/vg0-data磁盘时进行扩容(自动化脚本): 当新增物理磁盘时,自动 ...
- Windows2012 cannot access netapp CIFS share
NAS1> options cifs.smb2.signing.requiredcifs.smb2.signing.required off NAS1> options cifs.smb2 ...
- MSSQL update 多表关联更新
update tMeter set 字段= t.源自段 from ( select * from 源表信息 ) t where 关联条件 实际demo: UPDATE dbo.WX_TWODIMENC ...
- python 深入理解 赋值、引用、拷贝、作用域
在 python 中赋值语句总是建立对象的引用值,而不是复制对象.因此,python 变量更像是指针,而不是数据存储区域, 这点和大多数 OO 语言类似吧,比如 C++.java 等 ~ 1.先来看个 ...
- Redis应用案例,查找某个值的范围(转)
本文来自Redis在Google Group上的一个问题,有一位同学发贴求助,说要解决如下的一个问题:他有一个IP范围对应地址的列表,现在需要给出一个IP的情况下,迅速的查找到这个IP在哪个范围,也就 ...
- hdu 5444 Elven Postman 二叉树
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descrip ...
- linux常用命令 3
示例定义的 mytest或者test 用户 mygroup 用户组 cat /etc/group 查看组 groupname:x:groupId:其他成员 组名:x(加密):组ID:组成员cat /e ...