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变量和 ...
随机推荐
- Matcher类:(转)
Matcher类: 使用Matcher类,最重要的一个概念必须清楚:组(Group),在正则表达式中 ()定义了一个组,由于一个正则表达式可以包含很多的组,所以下面先说说怎么划分组的, 以及这 ...
- Gramar
一.And 并列关系(and) in addition / and / similarly / likewise / as well as / besides / furthermore / also ...
- Ubuntu用户相关基本命令
Linux是一个用户权限管理得很严格的系统,Ubuntu作为最受欢迎的桌面发行版,提供了简单易用的图形界面工具来管理用户,但是命令行工具往往更强大,用得熟练的话效率会更高.用户管理命令常用的有如下几个 ...
- submit回车提交影响
$(".bInput").bind('keydown',function(event){//回车提交手动标签 if(event.keyCode==13){ ...
- jquery插件理解看这
zepto 插件写法 一个更换背景颜色的小插件 html 1 <div id="box">content</div> javascript 12345678 ...
- [开发笔记]-Windows Service服务相关注意事项
注意一:报错:“本地计算机上的 *** 服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 该问题主要的原因是 Service服务程序中有错误. 遇到这个问题时,无论是重新安装服务,还是 ...
- Spring学习笔记之模块简介
1.Core Container(Application context) module 这个是Spring最基本的模块,它提供了spring框架最基本的功能.BeanFactory 是任何基于Spr ...
- bjui给出的一个标准应用的首页
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- ArrayList的使用方法【转载】
*** Source URL: http://i.yesky.com/bbs/jsp/view.jsp?articleID=889992&forumID=150 *** 1.什么是ArrayL ...
- poj1014 dp 多重背包
//Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...