java程序设计 面向对象基础 实验

Python013

java程序设计 面向对象基础 实验,第1张

二。

1。People.java

public class People {

//性别(sex)及出生日期(date);方法成员有:获取人的性别和出生日期及构造方法。要求构造方法可以设置性别和出生日期的初始值。

private int sex

private Date birth

public People (int sex, Date birth) {

this.sex = sex

this.birth = birth

}

public int getSex() {

return this.sex

}

public Date getBirth() {

return this.birth

}

}

2。Student.java

public class Student extends People{

private int sex

private Date birth

private String name

private int stuno

private double grate

private String studentNative

public Student(int sex, Date birth, String name, int stuno, double grate, String studentNative) {

super(sex, birth)

this.name = name

this.stuno = stuno

this.grate = grate

this.studentNative = studentNative

}

public Date getBirth() {

return birth

}

public double getGrate() {

return grate

}

public String getName() {

return name

}

public int getSex() {

return sex

}

public String getStudentNative() {

return studentNative

}

public int getStuno() {

return stuno

}

}

3。测试类自己编写就好了,创建一个People和Student的对象,然后设置值,取值就可以了。

五。

1.构造方法没有返回值,方法名和类名一样.

2.继承是指子类可以拥有父类的方法和属性;多态是指父类的引用可以指向子类对象的实例

3.重写

4.重载

其他的没什么了,自己完成以下吧。O(∩_∩)O~

package MyShape

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

Circle c = new Circle(2,4,3)

c.printItMyWay()

}

}

abstract class Shape{

public abstract float getCir()

public abstract float getArea()

}

class Point extends Shape implements Printable{

public int x

public int y

public Point(int x, int y){

this.x= x

this.y=y

}

public Point (){

}

@Override

public float getCir() {

// TODO Auto-generated method stub

return 0

}

@Override

public float getArea() {

// TODO Auto-generated method stub

return 0

}

public int getX() {

return x

}

public void setX(int x) {

this.x = x

}

public int getY() {

return y

}

public void setY(int y) {

this.y = y

}

@Override

public void printItMyWay() {

System.out.println(" Point ("+x+","+y+")")

System.out.println(" Point Area:"+this.getArea())

System.out.println(" Point Circle:"+this.getCir())

}

}

class Circle extends Point implements Printable{

public float r

public Circle(){

}

public Circle(float r,int x, int y ){

      if(r>0){

      this.r =r

      this.x =x

      this.y=y

}

}

public float getR() {

return r

}

public void setR(float r) {

this.r = r

}

@Override

public float getArea() {

return (float) (r*r*3.14/2)

}

@Override

public float getCir() {

return (float) (3.14*r*2)

}

@Override

public void printItMyWay() {

System.out.println(" Circle ("+x+","+y+")")

System.out.println(" Circle R:"+r)

System.out.println(" Circle Area:"+this.getArea())

System.out.println(" Circle Circle:"+this.getCir())

}

}

interface Printable {

public void printItMyWay()

}