Sixth week study Summary & (Experiment Report IV)

I. Experimental object
inherited methods (1) master class
(2) and the cover inheritance inheritance variables, methods, and coverage achieved overloaded
two experimental content

 

 

Experiment code:

package 实验三;
import java.util.Scanner;

class Circle {
    private double radius;
    private double perimeter;
    private double area;

    public Circle(){          
        this.setRadius(0);
    }
    public Circle(double r) {
        this.setRadius(r);
    }
    public double getRadius(){
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public void setPerimeter(double perimeter) {
        this.perimeter = perimeter;
    }
    public double getPerimeter() {
        return getRadius()*2*Math.PI;
    }
    public void setArea(double area) {
        this.area = area;
    }
    public double getArea() {
        return Math.PI*Math.pow(getRadius(),2);
    }
    public void disp(){
        System.out.println("圆的半径:"+getRadius());
        System.out.println("圆的周长:"+getPerimeter());
        System.out.println("圆的面积:"+getArea());
    }
}
    class Cylinder extends Circle{
        private double height ;
        public double getHeight(){
            return height;
        }
        public void setRadius(double height) {
            this.height = height;
        }
        Cylinder(double r,double h) {
            this.setRadius(r);
            this.setRadius(h);
        }
        public double getV() {
            return getArea()*height;
        }
        public void dispV(){
            System.out.println("圆柱的体积:"+getV());
        }
        public static void main(String[] args){
        Scanner s=new Scanner(System.in);
        System.out.println("请输入半径:");
        double r=s.nextInt ();                               
        System.out.println ( "Please enter High:" );
         Double H = s.nextInt ();                             
        Cylinder T = new new Cylinder (R & lt, H); 
        T.disp ();                                      
        T.dispV ( );   
    } 

}

 The results:

 

 

Programming Summary:

Class or to listen better, to do that in front of the rectangle, we already have some understanding of such procedures and the preparation of the foundation, in accordance with the week do question the idea to be no problem, but this week the teacher talked about a key set and get, playing code is also fast. But still appears on the succession of new knowledge, (main method can not be declared "static"; a static method can only be declared in a static type or top-level type), this is the problem I encountered in the experiment, Baidu, and my advice is to:

When you call in an external static method, you can use the "class name. Method name" approach, you can also use the "object name. Method name" approach. And examples of the latter method only way. In other words, you can call the static method without creating an object.

静态方法在访问本类的成员时,只允许访问静态成员(即静态成员变量和静态方法),而不允许访问实例成员变量和实例方法;实例方法则无此限制。
main()方法是一个典型的静态方法,它同样遵循一般静态方法的规则,所以它可以由系统在创建对象之前就调用。

似懂非懂,    public class Cylinder extends Circle{;问题就出现在这里,这里的public和后面的public static起冲突了,导致后面的静态无法声明,所以把前面的public删除后,就没什么问题了。但是写起来还是不怎么熟练。

学习总结:

 继承:在Java类中只允许单一继承,即一个子类只可以继承一个父类,且子类将继承父类的非私有属性和方法,但父类与子类只能一对一,即一个父类下面只有一个子类,但是那个子类可以作为父类进行下一次的继承。

 

方法的重载与复写:在重载中,方法名称相同,参数的类型或者个数不同,而覆盖都是相同的。覆盖只有发生在父类与子类之间,而重载可以发生在同一类中。 

super():

 1.super()从子类中调用父类的构造方法,this()在同一类内调用其它方法。

2.super()和this()均需放在构造方法内第一行,因此与this不能同时出现。

另外老师好像是还讲了instanceof和implements这两个关键字。

implements关键字在class声明中使用,以指示所声明的类提供了在implements关键字后面的名称所指定的接口中所声明的所有方法的实现。

instanceof关键字用来确定对象所属的类。

 

Guess you like

Origin www.cnblogs.com/Jay-h/p/11620334.html