メンバーのアクセスと継承

4649 ワード

package pratise;

//A class for two-dimensional objects
class TwoDShape{
    private double width;  //two are
    private double height; //now private
    
    //Accessor methods for width and height
    double getWidth() {return width;}
    double getHeight() {return height;}
    void setWidth(double w) {width = w;}
    void setHeight(double h) {height = h;}
    
    void showDim(){
        System.out.println("Width and Heigth are " + getWidth() + " and " + getHeight());
    }
}

//A subclass of TwoDShape for triangles
class Triangle extends TwoDShape{
    String style;
    double area(){
        return getWidth() * getHeight() / 2;
    }
    
    void showStyle(){
        System.out.println("Triangle is " + style);
    }
}

public class Construct {

    public static void main(String[] args) {
        Triangle t1 = new Triangle();
        Triangle t2 = new Triangle();
        
        t1.setWidth(4.0);
        t1.setHeight(4.0);
        t1.style = "filled";
        
        t2.setWidth(8.0);
        t2.setHeight(12.0);
        t2.style = "outlined";
        
        System.out.println("Info for t1: ");
        t1.showStyle();
        t1.showDim();
        System.out.println("Area is " + t1.area());
        System.out.println();
        
        System.out.println("Info for t2: ");
        t2.showStyle();
        t2.showDim();
        System.out.println("Area is " + t2.area());
    }
}
package pratise;

//A class for two-dimensional objects
class TwoDShape{
    private double width;  //two are
    private double height; //now private
    
    //Accessor methods for width and height
    double getWidth() {return width;}
    double getHeight() {return height;}
    void setWidth(double w) {width = w;}
    void setHeight(double h) {height = h;}
    
    void showDim(){
        System.out.println("Width and Heigth are " + getWidth() + " and " + getHeight());
    }
}

//A subclass of TwoDShape for triangles
class Triangle extends TwoDShape{
    private String style;
    
    //Constructor
    Triangle(String s, double w, double h){
        setWidth(w);
        setHeight(h);
        style = s;
    }
    
    double area(){
        return getWidth() * getHeight() / 2;
    }
    
    void showStyle(){
        System.out.println("Triangle is " + style);
    }
}

public class Construct {

    public static void main(String[] args) {
        Triangle t1 = new Triangle("filled", 4.0, 4.0);
        Triangle t2 = new Triangle("outlinde", 8.0, 8.0);
        
        System.out.println("Info for t1: ");
        t1.showStyle();
        t1.showDim();
        System.out.println("Area is " + t1.area());
        System.out.println();
        
        System.out.println("Info for t2: ");
        t2.showStyle();
        t2.showDim();
        System.out.println("Area is " + t2.area());
    }
}
package pratise;

//A class for two-dimensional objects
class TwoDShape{
    private double width;  //two are
    private double height; //now private
    
    //Parameterized constructor  
    TwoDShape(double w, double h){
        width = w;
        height = h;
    }
    
    
    //Accessor methods for width and height
    double getWidth() {return width;}
    double getHeight() {return height;}
    void setWidth(double w) {width = w;}
    void setHeight(double h) {height = h;}
    
    void showDim(){
        System.out.println("Width and Heigth are " + getWidth() + " and " + getHeight());
    }
}

//A subclass of TwoDShape for triangles
class Triangle extends TwoDShape{
    private String style;
    
    //subclass constructor
    Triangle(String s, double w, double h){
        super(w, h);  // super()   !!
        style = s;
    }
    
    double area(){
        return getWidth() * getHeight() / 2;
    }
    
    void showStyle(){
        System.out.println("Triangle is " + style);
    }
}

public class Construct {

    public static void main(String[] args) {
        Triangle t1 = new Triangle("filled", 4.0, 4.0);
        Triangle t2 = new Triangle("outlinde", 8.0, 8.0);
        
        System.out.println("Info for t1: ");
        t1.showStyle();
        t1.showDim();
        System.out.println("Area is " + t1.area());
        System.out.println();
        
        System.out.println("Info for t2: ");
        t2.showStyle();
        t2.showDim();
        System.out.println("Area is " + t2.area());
    }
}