java继承实例。

时间:2022-09-11 04:19:36

定义了一个点类point,然后线条类line继承了point类,正方形类Suare继承point类。

package test;

import javax.swing.*;
public class test {
public static void main(String args[]){
line AB = new line();
System.out.println("AB的第一个坐标为:x="+AB.EGetx()+",y="+AB.EGety()+"."+"AB的第二个坐标为:x="+AB.EGetx()+",y="+AB.FGety()+".");
Square ABCD = new Square();
System.out.println("ABCD的第一个坐标为:"+ABCD.Get1x()+","+ABCD.Get1y()+"."
+"ABCD的第二个坐标为:x="+ABCD.Get2x()+","+ABCD.Get2y()+"."
+"ABCD的第三个坐标为:x="+ABCD.Get3x()+","+ABCD.Get3y()+"."
+"ABCD的第四个坐标为:x="+ABCD.Get4x()+","+ABCD.Get4y()+".");
}
}
class point{
private int x,y;
static int pCount = 0;
point(){x=10;y=20;pCount++;System.out.println("创建一个point,这个point为第:"+pCount+"个.");}
point(int x,int y){this.x=x;this.y=y;pCount++;System.out.println("创建一个point,这个point为第:"+pCount+"个.");}
static int GeipCount(){return pCount;}
int Getx(){return x;}
int Gety(){return y;}
}
class line extends point{
static int lCount = 0;
private point E;
private point F;
private float range;
line()
{
E = new point();
F = new point(1,2);
range=1.5f;
lCount++;
System.out.println("创建一个line,这个line为第:"+lCount+"个.");
}
line(point E,point F,float range)
{
this.E=E;
this.F=F;
this.range=range;
lCount++;
System.out.println("创建一个line,这个line为第:"+lCount+"个.");
}
static int GeilCount(){return lCount;}
int EGetx(){return E.Getx();}
int EGety(){return E.Gety();}
int FGetx(){return F.Getx();}
int FGety(){return F.Gety();}
}
class Square extends point{
private point A;
private int range;
static int sCount = 0;
Square(){A = new point();range = 1;sCount++;System.out.println("创建一个Square,这个line为第:"+sCount+"个.");}
Square(point A){this.A = A;range = 1;sCount++;System.out.println("创建一个Square,这个line为第:"+sCount+"个.");}
Square(point A,int range){this.A = A;this.range = range;sCount++;System.out.println("创建一个Square,这个line为第:"+sCount+"个.");}
int Get1x(){return A.Getx();}
int Get1y(){return A.Gety();}
int Get2x(){return A.Getx()+range;}
int Get2y(){return A.Gety();}
int Get3x(){return A.Getx();}
int Get3y(){return A.Gety()+range;}
int Get4x(){return A.Getx()+range;}
int Get4y(){return A.Gety()+range;}
}