Java程序设计实验报告 继承与多态

Java程序设计实验报告 继承与多态,第1张

package MyShape;

public class Test {

/

@param args

/

public static void main(String[] args) {

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

cprintItMyWay();

}

}

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){

thisx= x;

thisy=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) {

thisx = x;

}

public int getY() {

return y;

}

public void setY(int y) {

thisy = y;

}

@Override

public void printItMyWay() {

Systemoutprintln(" Point ("+x+","+y+")");

Systemoutprintln(" Point Area:"+thisgetArea());

Systemoutprintln(" Point Circle:"+thisgetCir());

}

}

class Circle extends Point implements Printable{

public float r ;

public Circle(){

}

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

      if(r>0){

      thisr =r;

      thisx =x;

      thisy=y;

}

}

public float getR() {

return r;

}

public void setR(float r) {

thisr = r;

}

@Override

public float getArea() {

return (float) (rr314/2);

}

@Override

public float getCir() {

return (float) (314r2);

}

@Override

public void printItMyWay() {

Systemoutprintln(" Circle ("+x+","+y+")");

Systemoutprintln(" Circle R:"+r);

Systemoutprintln(" Circle Area:"+thisgetArea());

Systemoutprintln(" Circle Circle:"+thisgetCir());

}

}

interface Printable {

public void printItMyWay();

}

二。

1。Peoplejava

public class People {

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

private int sex;

private Date birth;

public People (int sex, Date birth) {

thissex = sex;

thisbirth = birth;

}

public int getSex() {

return thissex;

}

public Date getBirth() {

return thisbirth;

}

}

2。Studentjava

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);

thisname = name;

thisstuno = stuno;

thisgrate = grate;

thisstudentNative = 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~

import random

def rand6():

return int(randomrandom()6) + 1

def gamble(n):

roll = [rand6(), rand6(), rand6()]

bingo = rollcount(n)

if bingo ==0 :

return -1

else:

return bingo

n = 100000

money = 10000

for i in range(n):

money += gamble(rand6())

print(f'money left: {money}')

startChar = input('start:')

endChar = input('end:')

startOrd = ord(startChar)

endOrd = ord(endChar)

lineLen = endOrd - startOrd + 1

for i in range(startOrd, endOrd+1):

line = ''

for j in range(lineLen):

t = j + i

if t > endOrd:

t -= lineLen

line += chr(t)

print(line)

以上就是关于Java程序设计实验报告 继承与多态全部的内容,包括:Java程序设计实验报告 继承与多态、java程序设计 面向对象基础 实验、python程序设计实验怎么做做了一天了等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9286939.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-26
下一篇2023-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存