zoukankan      html  css  js  c++  java
  • Java 父类和子类

     1 package chapter11;
     2 
     3 public class GeometricObject1 {
     4     private String color="white";
     5     private boolean filled;
     6     private java.util.Date dateCreated;
     7     
     8     public GeometricObject1(){
     9         dateCreated=new java.util.Date();
    10     }
    11     
    12     public GeometricObject1(String color, boolean filled){
    13         dateCreated=new java.util.Date();
    14         this.color=color;
    15         this.filled=filled;
    16     }
    17     
    18     public String getColor(){
    19         return color;
    20     }
    21     public void setColor(String color){
    22         this.color=color;
    23     }
    24     
    25     public boolean isFilled(){
    26         return filled;
    27     }
    28     public void setFilled(boolean filled){
    29         this.filled=filled;
    30     }
    31     
    32     public java.util.Date getDateCreated(){
    33         return dateCreated;
    34     }
    35     
    36     public String toString(){
    37         return "created on "+dateCreated+"
    color: "+color+" and filled: "+filled;
    38     }
    39 
    40 }

    首先新建一个几何体类,作为父类,具有一般几何体的共性;

     1 package chapter11;
     2 
     3 public class Circle4 extends GeometricObject1 {
     4     private double radius;
     5     public Circle4(){
     6         
     7     }
     8     public Circle4(double radius){
     9         this.radius=radius;
    10     }
    11     public Circle4(double radius,String color,boolean filled){
    12         this.radius=radius;
    13         setColor(color);
    14         setFilled(filled);
    15     }
    16     
    17     public double getRadius(){
    18         return radius;
    19     }
    20     public void setRadius(double radius){
    21         this.radius=radius;
    22     }
    23     
    24     public double getArea(){
    25         return radius*radius*Math.PI;
    26     }
    27     public double getDiameter(){
    28         return 2*radius;
    29     }
    30     public double getPerimeter(){
    31         return 2*radius*Math.PI;
    32     }
    33     public void printCircle(){
    34         System.out.println("The circle is created "+getDateCreated()+
    35                 " and the radius is "+radius);
    36     }
    37 
    38 }
     1 package chapter11;
     2 
     3 public class Rectangle1 extends GeometricObject1 {
     4     private double width;
     5     private double height;
     6     
     7     public Rectangle1(){
     8         
     9     }
    10     public Rectangle1(double width,double height){
    11         this.width=width;
    12         this.height=height;
    13     }
    14     public Rectangle1(double width,double height,String color,boolean filled){
    15         this.width=width;
    16         this.height=height;
    17         setColor(color);
    18         setFilled(filled);
    19     }
    20     
    21     public double getWidth(){
    22         return width;
    23     }
    24     public void setWidth(double width){
    25         this.width=width;
    26     }
    27     
    28     public double getHeight(){
    29         return height;
    30     }
    31     public void setHeight(double height){
    32         this.height=height;
    33     }
    34     
    35     public double getArea(){
    36         return width*height;
    37     }
    38     public double getPerimeter(){
    39         return 2*(width+height);
    40     }
    41     
    42 
    43 }

    然后建立了基于父类的两个子类,Circle和Rectangle类,分别具有自己的数据域和方法属性,并实现。

     1 package chapter11;
     2 
     3 public class TestCircleRectangle {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         Circle4 circle=new Circle4(1);
     8         System.out.println("A circle "+circle.toString());
     9         System.out.println("The radius is "+circle.getRadius());
    10         System.out.println("The area is "+circle.getArea());
    11         System.out.println("The diameter is "+circle.getDiameter());
    12         
    13         Rectangle1 rectangle=new Rectangle1(2,4);
    14         System.out.println("
    A rectangle "+rectangle.toString()+"
    The area is "+
    15         rectangle.getArea()+"
    The perimeter is "+rectangle.getPerimeter());
    16 
    17     }
    18 
    19 }

    这是对子类的测试。

    总结:

    1、子类并不是父类的一个子集,实际上,一个子类通常比它的父类包含更多的信息和方法。

    2、父类中的私有数据域在该类之外不可访问,同样在子类中也不能直接使用,需要使用父类中的访问器/修改器来进行访问和修改。

    3、在Java中,不允许多重继承,一个Java类只能直接继承自一个父类,这种限制称为单一继承。多重继承可以通过接口来实现。

  • 相关阅读:
    Robot Framework环境搭建
    参数化登录QQ空间实例
    unittest单元测试框架总结(转载)
    判断弹出框存在(alert_is_ present)
    判断文本(text_to_be_present_in_element)
    判断title(title_is)
    判断元素(expected_conditions)
    等待页面元素(webdriverwait)
    unittest之断言
    unittest之装饰器
  • 原文地址:https://www.cnblogs.com/xingzhui/p/5705213.html
Copyright © 2011-2022 走看看