- 实验目的
- 理解面向对象编程基本思想;
- 掌握类和构造方法的定义;
- 理解静态和非静态成员的区别;
- 掌握创建类实例的方法;
- 掌握类的继承机制。
- 实验工具
- 编程语言:Python;
- 开发环境:Eclipse(或MyEclipse、NetBeans、IntelliJ IDEA等)。
- 实验题目
- 设计一个Dog类,有名字、颜色和年龄属性,定义构造方法初始化这些属性,定义输出方法show()显示其属性。
- (1) 编写一个学校类,其中包含成员变量scoreLine(录取分数线)和对该变量值进行设置和获取的方法。
(2) 编写一个学生类,它的成员变量有考生的name(姓名)、id(考号)、total(综合成绩)、sports(体育成绩)。它还有获取学生的综合成绩和体育成绩的方法。
(3) 编写一个录取类,它的一个方法用于判断学生是否符合录取条件。其中录取条件为:综合成绩在录取分数线之上,或者体育成绩在96以上并且综合成绩大于300。在该类的main()方法中,建立若干个学生对象,对符合录取条件的学生,输出其信息及“被录取”。
- 设计一个表示用户的类user,有用户名、口令(私有)和记录用户数(静态)的成员变量。定义类的构造方法、设置或获取口令的方法及返回类对象信息的方法。编写应用程序测试user类。
- (1) 设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x和y值的public方法。
(2) 设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r、获取和设置r值的public方法、计算圆面积的public方法。
(3) 设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体高的protected类型的成员变量h、获取和设置h值的public方法、计算圆柱体体积的public方法。
(4) 建立若干个Cylinder对象,输出其轴心位置坐标、半径、高及其体积的值。
- 编写一个程序,模拟电梯得功能。功能接口包括电梯上行按钮、下行按钮、楼层选择与电梯在行驶过程中得楼层显示。
(1) 由用户选择按上行按钮还就是下行按钮,选择操作后再由用户输入要进入得楼层,进而电梯开始运行,显示所到得每一楼层层数.
(2) 如果就是上行,则选择输入得楼层号不能比当前楼层号小,否则应给出不合法提示。
(3) 如果就是下行,则选择输入得楼层号不能比当前楼层号大,否则应给出不合法提示。
(4) 电梯一旦开始运作就会始终运行,直到窗口关闭。
(5) 电梯在经过不同楼层时,最好每个楼层得显示之间能有延迟,最终停靠得楼层得输出形式能更加醒目。如果可以,在电梯最初开始运行时,能在电梯由内部显示当前日期。
- 实验步骤
1. 安装JDK、Java集成式开发环境,并配置相应开发环境;
2. 按照题目要求,编写相应Java程序。
(请附上详细代码、运行截图等内容)
1. public class Dog {
String name;
String colo;
int age;
Dog(String name, String colo, int age) {
this.name = name;
this.colo = colo;
this.age = age;
}
void show()
{
System.out.println(name+" " + colo+" " + age+"");
}
}
class xg1{
public static void main(String[] args) {
Dog xg = new Dog("xg","白色",5);
xg.show();
}
}2.public class school {
int scoreLine;
void set(int scoreLine){
this.scoreLine=scoreLine;
}
int get(){
return scoreLine;
}
}
class stu{
String name;
int id;
int total;
int sports;
stu(String name,int id,int total,int sports){
this.id=id;
this.name=name;
this.sports=sports;
this.total=total;
}
int getTotal(){
return total;
}
int getSports(){
return sports;
}
}
class enter{
public static void main(String[] args){
enter a = new enter();
school NCHU = new school();
NCHU.set(550);
stu mxx = new stu("mxx",1,600,80);
stu mo = new stu("mo",2,500,70);
stu wjq = new stu("wjq",3,520,97);
a.tj(mxx, NCHU);
a.tj(mo,NCHU);
a.tj(wjq,NCHU);
}
public void tj(stu sch1,school sch2){
if(sch1.total>sch2.get()||(sch1.sports>96&&sch1.total>300))
System.out.println("被录取");
else
System.out.println("未被录取");
}
}3. public class user {
static int num;
user(String name){
this.name=name;
num++;
}
private String command;
String name;
void setCommand(String c){
this.command=c;
}
String getCommand(){
return this.command;
}
public static void main(String []args){
user a1=new user("周云天");
user a2=new user("吕布");
a1.setCommand("人中吕布");
a2.setCommand("马中赤兔");
System.out.println("创立了"+a1.num+"个对象");
System.out.println(a1.getCommand());
System.out.println(a2.getCommand());
}
}
4.public class Cylinder extends Circle{
protected int h;
void setH(int x){
this.h=x;
}
int getH(){
return this.h;
}
double getV(double x,int h){
return x*h;
}
public static void main(String []args){
Cylinder ti=new Cylinder();
ti.set(0,0);ti.setR(5);ti.setH(4);
System.out.println("体积:"+ti.getV(ti.area(), ti.getH()));
Cylinder ti1=new Cylinder();
ti1.set(2,3);ti1.setR(7);ti1.setH(6);
System.out.println("体积:"+ti1.getV(ti1.area(), ti1.getH()));
}
}
class Point{
protected int x;
protected int y;
public void set(int x,int y){
this.x=x;
this.y=y;
}
public String get(){
return String.valueOf(this.x)+" "+String.valueOf(this.y);
}
}
class Circle extends Point{
protected int r;
void setR(int x){
this.r=x;
}
int getR(){
return this.r;
}
double area(){
return 3.14*this.r*this.r;
}
}5.import java.util.Date;
public class elevator {
int choose;
int floor;
int now;
elevator(int c,int n,int f){
this.choose=c;
this.now=n;
this.floor=f;
}
void up(){
if(this.choose==1&&this.now<this.floor){
this.show();
}
else {
System.out.println("使用错误!");
}
}
void dowm(){
if(this.choose==-1&&this.now>this.floor){
this.show();
}
else {
System.out.println("使用错误!");
}
}
void show(){
System.out.print("当前时间:");
Date day=new Date();
System.out.println(day);
for(int i=this.now;i!=this.floor;){
try {
System.out.println(i);
Thread.sleep(1000);
}
catch (InterruptedException e){}
if(this.now>this.floor)
i--;
else
i++;
}
System.out.print("抵达抵达!");
System.out.println(this.floor+"楼");
}
public static void main(String []args){
elevator el1=new elevator(-1,15,10);
if(el1.choose==1)
el1.up();
else
el1.dowm();
}
}
- 实验心得
简要介绍你在实验中使用到的构造方法、静态成员变量、继承机制等的使用注意事项?
构造方法:构造方法与类名同名且不需要修饰词修饰
静态成员变量:静态成员变量是类的属性,只要是这个类的对象,这些对象的静态成员变量都是一样的。
继承机制:extends,父类与子类的构造方法会发生冲突,小心使用!