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 ...
随机推荐
- 【python】环境变量的配置
在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使用python命令. 1.首先需要在系统中注册python环境变量:假设python的安装路径为c:\p ...
- 【转】ASP.NET MVC 的最佳实践
[This post is based on a document authored by Ben Grover (a senior developer at Microsoft). It is ou ...
- LintCode "Heapify"
My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only fail ...
- Saltstack系列4:Saltstack之Grains组件
grains说明 grains是Saltstack最重要的组件之一,grains的作用是手机被控主机的基本信息,这些信息通常都是一些静态类的数据,包括CPU.内核.操作系统.虚拟化等,在服务器端可以根 ...
- 黄聪:深入理解PHP Opcode缓存原理
什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).Opcode cache的目地是避免重复编译,减少 ...
- Java compiler level does not match the version of the installed Java project facet. springmvc1 和 Target runtime Apache Tomcat v7.0 is not defined.
Java compiler level does not match the version of the installed Java project facet.springmvc1 : Targ ...
- DBA_Oracle冷备份案例脚本本法(案例)
2014-08-10 Created By BaoXinjian
- CMake使用教程
转自 RichardXG 原文 CMake使用教程 CMake是一个比make更高级的编译配置工具,它可以根据不同平台.不同的编译器,生成相应的Makefile或者vcproj项目. 通过编写CMak ...
- php socket 学习
socket超时设置 ini_set("default_socket_timeout", -1); stream_set_timeout $fp = fsockopen(" ...
- ScrollView--嵌套GridView的解决办法
前些日子在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全. 解决办法,自定义一个GridVie ...