类和对象实验

(一)分析和完善下面程序并打印输出结果,掌握类与对象的创建方法。
package sample;

public class StringTest {
    private String s;

    public void printString() {
        System.out.println(s);
    }

    public void changeString(String str) {
        s = str;
    }

    public static void main(String[] args) {
        // 完善你的代码
        StringTest st = new StringTest();
        st.changeString("Hello SZTU");
        st.printString();
    }
}

使用JDK的javac和java命令手动地编译和运行上面程序
在这里插入图片描述
在这里插入图片描述

(二)分析下面程序并打印输出结果,掌握继承关系和构造方法的调用,将分析情况在注释/**/之中。
package sample;
public class UseConstruct
{
	public static void main(String[] args)
	{  Manager m = new Manager("李力",6000,"学生院");//初始化赋值
       System.out.println(m.getSalary());
      }
}

class Employee 	 /*定义员工类*/
{     private String name;//名字
      private int salary;//薪水
      //构造函数
      public Employee(String _name, int _salary)
      {	   name = _name;
	       salary = _salary;
      }
      public String getSalary()   /*定义函数getSalary*/

      {
	    String str;
	    str = "名字: " + name + "\nSalary: " + salary;
       	return str;
       }
 }
class Manager extends Employee 	/*定义管理类,继承自员工类*/

{
	private String department;

//构造函数
	public Manager(String _name, int _salary, String _department)
	{
		super(_name,_salary);
		department = _department;
	}
/*重写getSalary函数,其中部分引用直接父类的函数getSalary*/
	public String getSalary() 
	{
		return super.getSalary() + "\nDepartment: " + department;
	}
}
(三)分析下面程序并打印输出结果,掌握类的封装特性。
package sample;
class EncapTest {
   int a; // 默认存取控制(default access)
   public int b; //公有存取控制(public access)
   private int c; //私有存取控制(private access)
   // 访问c的方法
   void setc(int i) {//设定c的值
     c = i;
   }
   int getc() { //获得c的值
     return c;
   }
}

public class AccessTest {
   public static void main(String args[]) {
     EncapTest ob = new EncapTest();
     // 以下代码是正确的
     ob.a = 10;
     ob.b = 20;
     // 以下代码会产生错误
     // ob.c = 30; 
     // 我们必须通过公有的方法操作c
     ob.setc(30); // 正确
     System.out.println("a,b,and c: " + ob.a + " " +ob.b + " " + ob.getc());
   }
}

  理解public和private对访问的作用,尝试将程序// ob.c = 30;开头的注释符号去掉,看看能否编译这个程序,进一步掌握封装的含义。 将程序// ob.c = 30;开头的注释符号去掉,不能编译这个程序。

(四)分析下面程序并打印输出结果,掌握类的继承特性和方法重写,将分析情况在注释/**/之中。
package sample;

public class TestExtend extends Employee	   //定义TestExtend类继承自Employee类

{
	public static void main(String[] args) 
	{
	   System.out.println("覆盖的方法调用:" + getSalary("王一",500));
       System.out.println("继承的方法调用:" + getSalary2("王一",500));
       System.out.println("覆盖的方法调用:" + getSalary("王飞",10000));
       System.out.println("继承的方法调用:” + getSalary2(“王飞",10000));
    }
    public static String getSalary(String name, int salary)		//重写方法getSalary

    {
	   String str;
	   if (salary>5000)
		 str = "名字: " + name + "    Salary: " + salary;
	   else
		  str = "名字: " + name + "    Salary: 低于5000";
	   return str;
     }
};

class Employee 
{
public String name;//名字
public int salary;//薪水
public static String getSalary(String name, int salary) 		//定义方法getSalary

{
	String str;
	str = "名字: " + name + "    Salary: " + salary;
	return str;
}
public static String getSalary2(String name, int salary) 		//定义方法getSalary2

{
	String str;
	str = "名字: " + name + "    Salary: " + salary;
    return str;
  }
}
(五)分析和完善下面程序并打印输出结果,掌握类的多态特性。
package sample;

public class ShapeTest {

  public ShapeTest() {
  }

  public static void main(String[] args) {
    Shape[] s = { new Shape(1, 4),
        new Rectangle(1, 2, 3, 4),
        new Circle(2, 3, 5) };
    for (int i = 0; i < s.length(); i++) {
      s[i].draw();
    }

  }

}

class Shape {
  protected int x;
  protected int y;

  public Shape() {
  }

  public Shape(int x, int y) {
    this.x = x;
    this.y = y;
  }

  public void draw() {
    System.out.println("This is a test in Shape.");
  }

}

class Rectangle extends Shape {
  private int heigth;
  private int weight;

  public Rectangle(int x, int y, int w, int h) {
    super(x, y);
    this.weight = w;
    this.heigth = h;

  }

  public void draw() {
    System.out.println("This is a test in Rectangle.");
  }
}

class Circle extends Shape {
  private int r;

  public Circle(int x, int y, int r) {
    super(x, y);
    this.r = r;

  }

  public void draw() {
    System.out.println("This is a test in Circle.");
  }
}
(六)编写Track类、Duration类 和Driver类。其中,Duration类包含三个属性:小时、分钟和秒,以及两个重载的构造方法;Track类包含两个属性:名称和长度(它是Duration对象类型),以及get/set方法;Driver类包含一个主方法,用来设定Track的长度和名称,然后把它们的值打印出来。
package example;

public class Track {
    private String title;
    private Duration duration;
    public Track() {
    }
    public Track(String title, Duration duration) {
        super();
        this.title = title;
        this.duration = duration;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Duration getDuration() {
        return duration;
    }
    public void setDuration(Duration duration) {
        this.duration = duration;
    }
    @Override
    public String toString() {
        return this.title + " " + this.duration.toString();
    }
}
package example;

public class Duration {
    private int hours;
    private int minutes;
    private int seconds;
    public Duration() {
    }
    public Duration(int totalSeconds) {
        this.hours = totalSeconds / 3600;
        this.minutes = (totalSeconds - this.hours * 3600) / 60;
        this.seconds = (totalSeconds - this.hours * 3600) % 60;
    }
    public Duration(int hors, int minutes, int seconds) {
        super();
        this.hours = hors;
        this.minutes = minutes;
        this.seconds = seconds;
    }
    public int getHours() {
        return hours;
    }
    public void setHour(int hours) {
        this.hours = hours;
    }
    public int getMinutes()
    {
        return minutes;
    }
    public void setMinutes(int minutes)
    {
        this.minutes=minutes;
    }
    public int getSeconds()
    {
        return seconds;
    }
    public void setSeconds(int seconds)
    {
        this.seconds=seconds;
    }
    @Override
    public String toString()
    {
        return this.hours+":"+this.minutes+":"+this.seconds;
    }
}
package example;

public class Driver {
    public static void main(String[] args) {
        Duration d = new Duration(200);
        Track t = new Track("They", d);
        System.out.println(d);
        System.out.println(t);
    }
}
(七)编写Shape类、Rectangle类和Circle类。其中Shape类是父类,其他两个类是子类。Shape类包含了两个属性:x和y,以及一个方法draw( );Rectangle类增加了两个属性;长度和宽度;Circle类增加了一个属性:半径。使用一个主方法来测试Shape中的数据和方法可以被子类继承。然后分别在两个子类中重写draw( ) 方法并实现多态。
package example;

public abstract class Shape extends Object {
    protected double x;
    protected double y;
    public Shape() {
    }
    public Shape(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double draw() {
        System.out.println("This is a test in Shape.");
    }
}
package example;

public class Rectangle extends Shape {
    private double width;
    private double length;
    public Rectangle() {
        this.length = 1;
        this.width = 1;
    }
    public double getWidth() {
        return width;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
    public Rectangle(double width, double length) {
        super();
        this.length = length;
        this.width = width;
    }
    public double draw() {
        System.out.println("This is a test in Rectangle.");
    }
}
package example;

public class Circle extends Shape {
    private double radius;
    public Circle() {
    }
    public Circle(double radius) {
        super();
        this.radius = radius;
    }
    public double getRadius() {
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    @Override
    public double draw() {
        System.out.println("This is a test in Circle.");
    }
}
(八)面向对象编程有哪3个基本概念?简单描述每一个。

1.封装、继承、多态;
2.封装:就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏;
3.继承:使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展;
4.多态:允许将子类类型的指针赋值给父类类型的指针。

(九)什么是类?什么是对象?类和对象有什么关系?

1.具有相同特性(数据元素)和行为(功能)的对象的抽象就是类;
2.对象是人们要进行研究的任何事物,它不仅能表示具体的事物,还能表示抽象的规则、计划或事件;

(十)假设有以下类
public class Test1{
  public  float aMethod(float a, float b){ }
 }

以下哪些方法可以合理地加入在第3行之前(多选)?(ABD)
A.public int aMethod(int a, int b){ }
B.public float aMethod(float a, float b){ }
C.public float aMethod(float a, float b, int c) throws Exception{ }
D.public float aMethod(float c, float d){}
E.private float aMethod(int a, int b, int c) { }

(十一)什么是继承?继承的特性可给面向对象编程带来什么好处?

继承实际上是存在与面向对象程序设计中的两个类之间的一种关系,是面向对象程序设计方法的一个重要手段,通过继承可以更有效第组织程序结构,明确类的关系,充分利用已有的类来完成更复杂、更深入的开发。

(十二)Java是否支持类之间的多重继承?

Java仅支持单重继承,即一个类至多只有一个直接父类。 在Java中通过接口来实现多重继承。

(十三)什么是多态?什么是RTTI?

①多态:通过运行时动态绑定, 动态判定当前引用的类型, 调用本类对应的函数,如果没有,则调用父类的函数。、
②RTTI:通过运行时类型信息程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值