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 ...
随机推荐
- Tomcat连接池
步骤1: 找到Tomcat安装目录下的context.xml文件,在config目录下.在<Context/>节点下加入: <Resource name="jdbc/myt ...
- HackerRank "Minimum Average Waiting Time" !
Something to learn: http://blog.csdn.net/yuwenshi/article/details/36666453 Shortest Job First Algori ...
- Lucene.Net 站内搜索
Lucene.Net 站内搜索 一 全文检索: like查询是全表扫描(为性能杀手)Lucene.Net搜索引擎,开源,而sql搜索引擎是收费的Lucene.Net只是一个全文检索开发包(只是帮我们 ...
- 剑指offer系列45---和为s的两个数字
[题目]输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S, package com.exe9.offer; /** * [题目]输入一个递增排序的数组和一个数字S,在数组中 ...
- ORACLE Instant Client 配置
http://www.oracle.com/technetwork/cn/database/features/instant-client/index-092699-zhs.html 在官网下载对应的 ...
- 【Linux】系统之vmstat&iostat
Linux系统出现了性能问题,一般我们可以通过top.iostat.free.vmstat等命令来查看初步定位问题. iostat常见用法: $iostat -d -k 1 10 #查看TPS和吞吐量 ...
- Spark on Yarn 架构解析
. 一.Hadoop Yarn组件介绍: 我们都知道yarn重构根本的思想,是将原有的JobTracker的两个主要功能资源管理器 和 任务调度监控 分离成单独的组件.新的架构使用全局管理所有应用程序 ...
- linux命令(6)crontab的用法和解析
一,写入格式: * * * * * command minute hour day month week command 其中: minute: 表示分钟,可以是从0到59之间 ...
- Hololens开发笔记之Gesture手势识别(基本介绍)
手势识别是HoloLens交互的重要输入方法之一.HoloLens提供了底层API和高层API,可以满足不同的手势定制需求.底层API能够获取手的位置和速度信息,高层API则借助手势识别器来识别预设的 ...
- (转)ZooKeeper伪分布式集群安装及使用
转自:http://blog.fens.me/hadoop-zookeeper-intro/ 前言 ZooKeeper是Hadoop家族的一款高性能的分布式协作的产品.在单机中,系统协作大都是进程级的 ...