5 changed files with 157 additions and 0 deletions
@ -0,0 +1,42 @@ |
|||||
|
package Person; |
||||
|
|
||||
|
abstract class Person{ |
||||
|
protected String name; |
||||
|
abstract void add(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
class Student extends Person{ |
||||
|
String studentId;//定义属性
|
||||
|
double score; |
||||
|
public Student(String studentId,String name,double score){ |
||||
|
this.studentId=studentId; |
||||
|
this.name=name; |
||||
|
this.score=score; |
||||
|
} |
||||
|
@Override |
||||
|
void add(){ |
||||
|
System.out.println("已成功添加学生 "+studentId+" "+name+" "+score); |
||||
|
} |
||||
|
} |
||||
|
class Teacher extends Person{ |
||||
|
String teacherId; |
||||
|
String subject; |
||||
|
public Teacher(String teacherId,String name,String subject){ |
||||
|
this.subject=subject; |
||||
|
this.name=name; |
||||
|
this.teacherId=teacherId; |
||||
|
} |
||||
|
@Override |
||||
|
void add(){ |
||||
|
System.out.println("已成功添加老师 "+teacherId+" "+name+" "+subject); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Add{ |
||||
|
public static void addPerson(Person p){ |
||||
|
p.add(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
@ -0,0 +1,13 @@ |
|||||
|
package Person; |
||||
|
|
||||
|
public class PersonTest { |
||||
|
public static void main (String[] args) { |
||||
|
//setting up the first object
|
||||
|
Person s1=new Student("0001","Roya",90); |
||||
|
//setting up the second object
|
||||
|
Person t1=new Teacher("001","Edward","Biology"); |
||||
|
//using the approach addPerson()
|
||||
|
Add.addPerson(s1); |
||||
|
Add.addPerson(t1); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
package Shape; |
||||
|
|
||||
|
abstract public class Shape { |
||||
|
abstract void draw(); |
||||
|
} |
||||
|
class Circle extends Shape{ |
||||
|
private double r; |
||||
|
public Circle(double r){ |
||||
|
this.r=r; |
||||
|
} |
||||
|
@Override |
||||
|
public void draw(){ |
||||
|
System.out.println("已绘制半径为"+r+"的圆"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Rectangle extends Shape{ |
||||
|
private double chang; |
||||
|
private double kuan; |
||||
|
public Rectangle(double chang,double kuan){ |
||||
|
this.chang=chang; |
||||
|
this.kuan=kuan; |
||||
|
} |
||||
|
@Override |
||||
|
public void draw(){ |
||||
|
System.out.println("已绘制长宽分别为"+chang+"和"+kuan+"的矩形"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Triangle extends Shape{ |
||||
|
private double di; |
||||
|
private double height; |
||||
|
public Triangle(double di,double height){ |
||||
|
this.di=di; |
||||
|
this.height=height; |
||||
|
} |
||||
|
@Override |
||||
|
public void draw(){ |
||||
|
System.out.println("已绘制底和高分别为"+di+"和"+height+"的三角形"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class ShapeUtil{ |
||||
|
public static void drawShape(Shape s){ |
||||
|
s.draw(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
package Shape; |
||||
|
|
||||
|
public class ShapeTest { |
||||
|
public static void main(String[] args){ |
||||
|
System.out.println("--测试三个类--"); |
||||
|
//circle
|
||||
|
Circle round=new Circle(5); |
||||
|
Rectangle b=new Rectangle(4,7); |
||||
|
Triangle c=new Triangle(8,4); |
||||
|
ShapeUtil.drawShape(round); |
||||
|
ShapeUtil.drawShape(b); |
||||
|
ShapeUtil.drawShape(c); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
package vehicle; |
||||
|
|
||||
|
abstract class vehicle { |
||||
|
abstract void run(); |
||||
|
} |
||||
|
|
||||
|
class Bike extends vehicle{ |
||||
|
public String model; |
||||
|
public Bike(String model){ |
||||
|
this.model=model; |
||||
|
} |
||||
|
@Override |
||||
|
public void run(){ |
||||
|
System.out.println("已存放一辆"+model+"脚踏车"); |
||||
|
} |
||||
|
} |
||||
|
class Car extends vehicle{ |
||||
|
public String model; |
||||
|
public Car(String model){ |
||||
|
this.model=model; |
||||
|
} |
||||
|
@Override |
||||
|
public void run(){ |
||||
|
System.out.println("已存放一辆"+model+"汽车"); |
||||
|
} |
||||
|
} |
||||
|
class Truck extends vehicle{ |
||||
|
public String model; |
||||
|
public Truck(String model){ |
||||
|
this.model=model; |
||||
|
} |
||||
|
@Override |
||||
|
public void run(){ |
||||
|
System.out.println("已存放一辆"+model+"卡车"); |
||||
|
} |
||||
|
} |
||||
|
class RunVehicle{ |
||||
|
public void Run(vehicle v){ |
||||
|
v.run(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue