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 ...
随机推荐
- LintCode "Expression Tree Build"
Lesson learnt: for any calculator problems, keep 2 stacks: 1 for operators and 1 for operands. class ...
- Python控制流语句(if,while,for)
if.py number=23 guess=int(input("enter an int:")) if guess==number: print ("congratul ...
- sublime text 3 安装注释
sublime text 3 1.安装Sublime Text 3 下载安装:http://www.sublimetext.com/3 Package Control安装:https://subli ...
- Shell_Shell调用SQLPlus简介(案例)
2014-06-20 Created By BaoXinjian
- php 消息队列
本消息队列用于linux下,进程通信 #根据路径和后缀创建一个id $key = ftok(__DIR__, 'R'); #获取队列中的消息 $q = msg_get_queue($key); #删除 ...
- 转载__Java内部类
内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的一个成员,并且依附于外部类而存在的.内部类可为静态,可用protected和private修饰(而外部类只能使用public和缺省的包访问权 ...
- 三、jdk工具之jstack(Java Stack Trace)
目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...
- Java中-XMX -xmn 是什么的缩写
这个应该是 eclipse 的配置文件 eclipse.ini 中的配置语句.在配置文件中直接传递给 java vm 的参数并不多,调用形式是这样的: 1 eclipse [normal argume ...
- Liferay中SQL打印参数
XX\tomcat-7.0.42\webapps\ROOT\WEB-INF\classes\log4j.properties log4j.rootLogger=INFO, CONSOLE log4 ...
- 【转】深入理解DIP、IoC、DI以及IoC容器
原文链接:http://www.cnblogs.com/liuhaorain/p/3747470.html 前言 对于大部分小菜来说,当听到大牛们高谈DIP.IoC.DI以及IoC容器等名词时,有没有 ...