二。
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~
importjavaio;
importjavaioFile;
importjavaioFileReader;
importjavaioFileWriter;
importjavaioIOException;
publicclassNewTxt{
publicstaticvoidmain(String[]args)throwsIOException{
intn=5;//NN数组
double[][]arr=newdouble[n][n];//插入的数组
double[][]arr2=newdouble[n][n];;//读取出的数组
//数组初始化,随机生成的[0,100)之间的double数
for(inti=0;i
for(intj=0;j
arr[i][j]=Mathrandom()100;
Systemoutprintln(arr[i][j]);
}
}
Filefile=newFile("d:\arraytxt");//存放数组数据的文件
FileWriterout=newFileWriter(file);//文件写入流
//将数组中的数据写入到文件中。每行各数据之间TAB间隔
for(inti=0;i
for(intj=0;j
outwrite(arr[i][j]" ");
}
outwrite("");
}
outclose();
in=new(newFileReader(file));//
Stringline;//一行数据
introw=0;
//逐行读取,并将每个数组放入到数组中
while((line=inreadLine())!=null){
String[]temp=linesplit(" ");
for(intj=0;j
arr2[row][j]=DoubleparseDouble(temp[j]);
}
row;
}
inclose();
//显示读取出的数组
for(inti=0;i
for(intj=0;j
Systemoutprint(arr2[i][j]" ");
}
Systemoutprintln();
}
}
}
自己写的,也才学不容易还望采纳
import javautil;
abstract class Shape{
final static double PI=314;
public abstract double getPerimeter();
public abstract double getArea();
public static double sumAllArea(Shape []shapes) {
double x=0;
for(Shape e:shapes) {
x+=egetArea();
}
return x;
}
public static double sumAllPerimeter(Shape []shapes) {
double y=0;
for(Shape e:shapes) {
y+=egetPerimeter();
}
return y;
}
}
class Rectangle extends Shape{
private int width;
private int length;
public double getPerimeter() {
return 2(width+length);
}
public double getArea() {
return widthlength;
}
public Rectangle(int width,int length) {
thiswidth=width;
thislength=length;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}
}
class Circle extends Shape{
private int radius;
public double getPerimeter() {
// TODO Auto-generated method stub
return 2PIradius;
}
public double getArea() {
// TODO Auto-generated method stub
return PIradiusradius;
}
public Circle(int radius) {
thisradius=radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(Systemin);
int n;
n=innextInt();
Shape shapes[]=new Shape[n];
for(int i=0;i<n;i++) {
String s=innext();
switch(s) {
case "rect":
int width,length;
width=innextInt();
length=innextInt();
shapes[i]=new Rectangle(width, length);
break;
case "cir":
int radius;
radius=innextInt();
shapes[i]=new Circle(radius);
break;
}
}
Systemoutprintln(ShapesumAllPerimeter(shapes));
Systemoutprintln(ShapesumAllArea(shapes));
Systemoutprintln(ArraystoString(shapes));
for(int i=0;i<n;i++) {
Systemoutprintln(shapes[i]getClass()+","+shapes[i]getClass()getSuperclass());
}
}
}
以上就是关于java程序设计 面向对象基础 实验全部的内容,包括:java程序设计 面向对象基础 实验、Java程序设计(java程序设计答案)、7-1 jmu-Java-03面向对象基础-04-形状-继承(15 分)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)