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 ...
随机推荐
- [转]使用 Minidumps 和 Visual Studio .NET 进行崩溃后调试
本文关键字:Minidumps, Windows, SEH, VisualC, .NET 摘要 本文讲述了 minidumps 是怎样工作的.当你的程序崩溃的时候应该如何生成它们.以及如何在 Visu ...
- 【Spring-AOP-1】AOP相关概念
Advice (好多中文书籍翻译为:增强处理,比如前向增强.后向增强等) 描述了Aspect类执行的具体动作.the job of an aspect. 定义了如下两个方面: what:即Aspect ...
- zend studio 12汉化和破解
首先提供一个 zend studio 12汉化的百度连接地址(我的网盘里有) http://pan.baidu.com/s/1dD5x1cd 下载后解压 安装方法 Help–> Install ...
- Enable EPEL Repository for RHEL/CentOS 7.x/6.x/5.x
This howto guide shows you’ll how to enable EPEL repository under RHEL/CentOS 6/5 to install additio ...
- 有关JSP注释
最近学习过滤器的时候,dispatcher可以指定过滤器被Servlet容器拦截的方式,可以是REQUEST.INCLUDE.FORWARD.ERROR,默认是REQUEST方式. 现在有两个filt ...
- byte[] 与字符串转换
//取值之后进行 StringBuffer buffer=new StringBuffer(); for (int i = 0; i < enBytes.length; i++) { if(i! ...
- GTD_百度百科
GTD就是Getting Things Done的缩写,翻译过来就是"把事情做完",GTD的核心理念概括就是必须记录下来要做的事,然后整理安排并自己一一去执行.GTD的五个核心原则 ...
- python 读取sqlite3 数据库
import sqlite3 name = "tom" age = 30 con = sqlite3.connect("d:\\test.db") cur = ...
- IO - IOUtils
Commons IO is a library of utilities to assist with developing IO functionality. There are four main ...
- android listView Exception
- ::-/com.tongyan.tutelage W/System.err﹕ java.text.ParseException: Unparseable date: ) - ::-/com.ton ...