Java 父类和子类
package chapter11;
public class GeometricObject1 {
private String color="white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject1(){
dateCreated=new java.util.Date();
}
public GeometricObject1(String color, boolean filled){
dateCreated=new java.util.Date();
this.color=color;
this.filled=filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled=filled;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
public String toString(){
return "created on "+dateCreated+"\ncolor: "+color+" and filled: "+filled;
}
}
首先新建一个几何体类,作为父类,具有一般几何体的共性;
package chapter11;
public class Circle4 extends GeometricObject1 {
private double radius;
public Circle4(){
}
public Circle4(double radius){
this.radius=radius;
}
public Circle4(double radius,String color,boolean filled){
this.radius=radius;
setColor(color);
setFilled(filled);
}
public double getRadius(){
return radius;
}
public void setRadius(double radius){
this.radius=radius;
}
public double getArea(){
return radius*radius*Math.PI;
}
public double getDiameter(){
return 2*radius;
}
public double getPerimeter(){
return 2*radius*Math.PI;
}
public void printCircle(){
System.out.println("The circle is created "+getDateCreated()+
" and the radius is "+radius);
}
}
package chapter11;
public class Rectangle1 extends GeometricObject1 {
private double width;
private double height;
public Rectangle1(){
}
public Rectangle1(double width,double height){
this.width=width;
this.height=height;
}
public Rectangle1(double width,double height,String color,boolean filled){
this.width=width;
this.height=height;
setColor(color);
setFilled(filled);
}
public double getWidth(){
return width;
}
public void setWidth(double width){
this.width=width;
}
public double getHeight(){
return height;
}
public void setHeight(double height){
this.height=height;
}
public double getArea(){
return width*height;
}
public double getPerimeter(){
return 2*(width+height);
}
}
然后建立了基于父类的两个子类,Circle和Rectangle类,分别具有自己的数据域和方法属性,并实现。
package chapter11;
public class TestCircleRectangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle4 circle=new Circle4(1);
System.out.println("A circle "+circle.toString());
System.out.println("The radius is "+circle.getRadius());
System.out.println("The area is "+circle.getArea());
System.out.println("The diameter is "+circle.getDiameter());
Rectangle1 rectangle=new Rectangle1(2,4);
System.out.println("\nA rectangle "+rectangle.toString()+"\nThe area is "+
rectangle.getArea()+"\nThe perimeter is "+rectangle.getPerimeter());
}
}
这是对子类的测试。
总结:
1、子类并不是父类的一个子集,实际上,一个子类通常比它的父类包含更多的信息和方法。
2、父类中的私有数据域在该类之外不可访问,同样在子类中也不能直接使用,需要使用父类中的访问器/修改器来进行访问和修改。
3、在Java中,不允许多重继承,一个Java类只能直接继承自一个父类,这种限制称为单一继承。多重继承可以通过接口来实现。
Java 父类和子类的更多相关文章
- java父类转子类的一个方法
一般子类可以转父类.但父类转子类就会报cast error. 使用jsonobject 思想:先把父类转jsonstring 再把jsonstring转子类.剩余的子类值可以设定进去. import ...
- Java父类与子类方法调用顺序
父类 FatherClass package 父类与子类方法调用顺序; /** * 父类 * @author shundong * */ public class FatherClass { priv ...
- JAVA 父类与子类初始化顺序问题
main方法-->子类对象的初始化语句(new className()语句)--->子类构造[因为继承的缘故,它先不会执行]--->父类构造[这一步先不会执行]--->父类静态 ...
- Java父类与子类中静态代码块 实例代码块 静态变量 实例变量 构造函数执行顺序
实例化子类时,父类与子类中的静态代码块.实例代码块.静态变量.实例变量.构造函数的执行顺序是怎样的? 代码执行的优先级为: firest:静态部分 second:实例化过程 详细顺序为: 1.父类静态 ...
- 【java基础】(2)Java父类与子类的 内存引用讲解
从对象的内存角度来理解试试.假设现在有一个父类Father,它里面的变量需要占用1M内存.有一个它的子类Son,它里面的变量需要占用0.5M内存.现在通过代码来看看内存的分配情况:Father f = ...
- [Java] 父类和子类拥有同名的成员变量(fields)的情况
首先,需要明确的是,无论是通过casting,还是通过将子类对象的reference赋值给父类变量,都无法改变该reference所指对象的真实类型.但当该reference的类型是父类时,将无法调用 ...
- Java特性之多态父类与子类之间的调用
问题描述: Java三大特性,封装.继承.多态,一直没搞懂其中多态是什么,最近研究了一下,关于父类和子类之间的调用.下面是一个测试类,源代码如下: package com.test; public c ...
- java中父类与子类, 不同的两个类中的因为构造函数由于递归调用导致栈溢出问题
/* 对于类中对成员变量的初始化和代码块中的代码全部都挪到了构造函数中, 并且是按照java源文件的初始化顺序依次对成员变量进行初始化的,而原构造函数中的代码则移到了构造函数的最后执行 */ impo ...
- Java父类子类的对象初始化过程
摘要 Java基本的对象初始化过程,子类的初始化,以及涉及到父类和子类的转化时可能引起混乱的情况. 1. 基本初始化过程: 对于一个简单类的初始化过程是: static 修饰的模块(static变量和 ...
随机推荐
- Ext grid checkbox 分页 翻页 勾选 问题
timeArray = new Array(); //临时数组变量 var timeStatusBar = new Ext.ux.StatusBar({ id: 'statusbar', defaul ...
- PowerPivot安装完成后创建网站或网站集报错解决办法
根据上一篇“在现有 SharePoint 服务器上安装 PowerPivot for SharePoint”后,新建网站或网站集时报错,重新配置了一下PowerPivot For SharePoint ...
- plsql快速选中一行的快捷键
实际工作中,经常用到pl/sql,在sql window中,经常性的用到选中一行然后按F8执行这条sql语句.用鼠标选中一行不是特别方便.用快捷键就快多了. 1.使用home键(不是windows键奥 ...
- 关于HashMap中的负载因子
这两天在看HashMap的时候,被负载因子float loadFactor搞得很晕,经过一天的研究,最后理出了自己的一点个人见解. 在HashMap的底层存在着一个名字为table的Entry数组,在 ...
- LIS-Program E
最大上升子序列 Description The world financial crisis is quite a subject. Some people are more relaxed whil ...
- JAVA小记
关于重写equals()方法和重写toString()方法,一般来说,Objects的默认子类都重写了这两个方法,直接利用就行了: 对于用户自定义的类,如果要用到这两方法,就必须在程序中重写.
- AndroidStudio导入第三方开源库 --文件夹源码
1 在已打开的项目中 File-New-ImportModule 选择开源项目中的 库所在文件夹比如 library文件夹 然后导入. 2 File-Project Sructure 在Modu ...
- dotTracePerormance 工具
今天凌晨 阿根廷对瑞士比赛已经过去,比分是1:0 阿根廷获胜:虽说我是伪球迷,但是也挺希望梅西进入决赛.昨晚也压了下90分之内 0:0 ,结果胜出:另一场压的是美国对比利时,也是压平,就这样二串 ...
- C#注册表常用操作
1:加键 改值 Microsoft.Win32.RegistryKey Key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( @" ...
- 提示错误#165 too few argument in function call
调用函数时,参数个数少于函数定义.检查一下函数定义和参数调用,两个要一致.