Java实验6(07) 类与对象
- 格式:doc
- 大小:433.50 KB
- 文档页数:8
班级: 姓名: 学号:
面向对象编程基础(一)简单的类和对象
实验目的:
1. 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
2. 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
实验数据记录及分析(或程序及运行结果)
1. 写一个名为Rectangle的类表示同一种颜色的矩形类。其成员属性包括宽width、高height,类属性包括颜色color(默认颜色是蓝色),width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 合理的构造函数,完成各属性的初始赋值
(2) 使用get和set的形式完成各个属性的访问及修改
(3) 计算面积的getArea()方法
(4) 合理的toString方法。
//注解:成员属性包括宽width、高height指的是每个矩形对象的宽和高均不一样
// 类属性包括颜色color指的是每个矩形对象的颜色都一样(即static成员)
主函数如下:
public class Rectangle {
private double width, height;
private static String color = "蓝色";
public Rectangle() {
this(0.0, 0.0); //调用本类的构造函数,即下面两个参数的构造函数
}
public Rectangle(double width) {
this(width, width); //调用本类的构造函数,即下面两个参数的构造函数
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
} public static String getColor() { //注意有static
return color;
}
public static void setColor(String color) { //注意有static
Rectangle.color = color;
}
public double getArea() {
return width * height;
}
public String toString() {
return "宽: " + width + "\t\t高:" + height + "\t\t颜色: " +
color + "\t\t面积: " + getArea();
}
public static void main(String[] args) {
Rectangle r;
System.out.println("创建一个默认初值的矩形:");
r = new Rectangle();
System.out.println("\t" + r);
System.out.println("修改具有默认初值矩形的宽为10,高为20:");
r.setWidth(10);
r.setHeight(20);
System.out.println("\t" + r);
System.out.println("修改所有矩形对象的颜色为红色");
Rectangle.setColor("红色");
System.out.println("\t" + r);
System.out.println("创建一个宽10,高30的矩形");
r = new Rectangle(10, 30);
System.out.println("\t" + r);
System.out.println("创建一个边长为1的正方形:");
r = new Rectangle(1);
System.out.println("\t" + r);
}
}
2. 一副牌Deck有52张扑克Card组成(不含王牌),每张牌有自己的牌型suit(用char类型)和点数rank(用String类型),补充完整下面的类的定义。
public class Card {
char suit;
String rank;
public Card( char suit,String rank ) {
this.suit = suit;
this.rank = rank;
}
public String toString() { return suit+rank;
}
public static void main(String[] args){
Card c=new Card('红',"10");
System.out.println(c.toString); //红10
System.out.println(c); //此代码意思同上 红10
}
}
3. 程序运行后的输出是什么?
class TestReference{
public static void main(String[] args){
int x=2;
TestReference tr = new TestReference();
System.out.print(x);
tr.change(x);
System.out.print(x);
}
public void change(int num){
num = num + 1;
}//方法调用结束没有打印也没有返回,释放局部变量num
}
22
4. 写出程序运行结果
public class Foo {
public static void main (String [] args) {
StringBuilder a = new StringBuilder (“A”);
StringBuilder b = new StringBuilder (“B”);
operate(a,b);//调用了Foo类的一个类方法
System.out.println(a + “,” +b);
}
static void operate (StringBuilder x, StringBuilder y) {
x.append(y);
y = x; //y指向x所指向的对象
}//方法调用结束没有打印也没有返回,释放局部变量x和y
}
AB,B
5. 解释下面的程序运行结果输出为什么是null
public class My {
String s; //null
public void My(){
s = "Constructor";
} public void go() {
System.out.println(s);
}
public static void main(String args[]) {
My m = new My();
m.go();
}
}
因为
public void My(){
s = "Constructor";
}
方法有声明返回值类型为void,所以不是构造方法,因此My类仅有默认的构造方法,所以s被初始化为null.
6. 分析程序
class TestApp{
public static void main(String[] args){
System.out.println(multiply(2,3,4,5));
}
public int multiply(int … nums){//变长参数
int result = 1;
for(int x :nums)
result *= x; //result=1*2*3*4*5
return result;
}
}
能否正常运行?原因是什么?
不行,因为multiply方法没有声明为static,
所以在static主函数被调用时需要由一个对象来引用。
7. 给出下面的类,找出后面的5个声明中,哪些是重载后的构造函数
public class ConstOver {
public ConstOver (int x, int y, int z) {
}
}
A. ConstOver ( ) { }
B. Protected int ConstOver ( ) { }
C. Private ConstOver (int z, int y, byte x) { }
D. Public Object ConstOver (int x, int y, int z) { }
E. Public void ConstOver (byte x, byte y, byte z) { }