大白糖奶兔的Blog
大白糖奶兔的Blog
Java学习-Day09类的组合
Java学习-Day09类的组合

将已存在类的对象放到新类中

package 第三章类的重用;

public class 类的组合 {
    public static class Point{
        private int x,y;
        public Point(int x,int y){this.x=x;this.y=y;}
        public int GetX(){return x;}
        public int GetY(){return y;}
    }

    static class Line{
        //    点类的构造方法,构造两个点对象
        private Point p1,p2;
        Line(Point a,Point b){
            p1 = new Point(a.GetX(),a.GetY());
            p2 = new Point(b.GetX(),b.GetY());
        }
        public double Length(){
            return (Math.sqrt(Math.pow(p2.GetX()-p1.GetX(),2) + Math.pow(p2.GetY()-p1.GetY(),2)));
        }
    }

    public static void main(String[] args) {
        Point p1=new Point(1,2);
        Point p2=new Point(3,4);
        Line l1=new Line(p1,p2);
    }
}

发表评论

textsms
account_circle
email

大白糖奶兔的Blog

Java学习-Day09类的组合
将已存在类的对象放到新类中 package 第三章类的重用; public class 类的组合 { public static class Point{ private int x,y; public Point(int x,int y){this.…
扫描二维码继续阅读
2020-03-07
Title - Artist
0:00