• 沒有找到結果。

Java程序设计实训指导 - 万水书苑-出版资源网

N/A
N/A
Protected

Academic year: 2021

Share "Java程序设计实训指导 - 万水书苑-出版资源网"

Copied!
7
0
0

加載中.... (立即查看全文)

全文

(1)实训 5 面向对象编程练习. 实训. 5. 面向对象编程练习 5.1 实训目的. 通过编程和上机实验理解 Java 语言是如何体现面向对象编程基本思想,以及如何创建类 和对象,了解成员变量和成员方法的特性。. 5.2 实训要求 . 编写一个体现面向对象思想的程序。. . 编写一个创建对象和使用对象的方法的程序。. 5.3 实训内容. 5.3.1. 创建对象并使用对象. (1)定义一个 Person 类,可以在应用程序中使用该类。 成员属性:Person 类的属性(变量) : 姓名:name,字符串类型:String; 性别:sex,字符型:char; 年龄:age,整型:int。 3 个构造函数: public Person(String s). //设置姓名. public Person(String s,char c). //调用本类的构造函数 Person(String s),设置性别. public Person(String s,char c,int i). //调用本类的构造函数 Person(String s,char c),设置年龄. 一个成员方法: public String toString() //获得姓名、性别和年龄. 利用定义的 Person 类,请实例化对象,输出下面结果:.

(2) 24. Java 程序设计实训指导. 姓名:张三. 性别:男. 年龄:21. 把下面程序补充完整,并调试通过写出结果。 public class Person { private String name; private char sex; private int age; public Person(________){ name =________; } public Person(String s, ________){ name = s; sex =________; } public Person(String s,char c, ________){ name = s; sex = c; age =________; } public String toString(){ if((sex=='M')||(sex=='m')) ________ "姓名:"+name+" 性别:男"+". 年龄:"+age;. else if((sex== 'W')||(sex=='w')) return "姓名:"+name+" 性别:女"+" 年龄:"+age; else return "性别有误!"; } public static void main(String[] args){ ________ p1=________ Person("张三",'M',21); System.out.println(p1. ________); } }. 图 5-1 是程序输出结果。. 图 5-1 person 类运行结果. (2)当参数为对象时,传递的方式为引用传递,运行下面程序,运行结果如图 5-2 所示。 class Test { int a,b; Test(int i,int j) { a=i; b=j; }.

(3) 实训 5 面向对象编程练习. // 传递一个对象 void method(Test ob) { ob.a = ob.a+10; ob.b = ob.b-10; } } public class CallByReference { public static void main(String args[]) { Test object = new Test(25,30); System.out.println("调用前 object.a 和 object.b 的值:" + object.a + " " + object.b); object.method(object); System.out.println("调用后 object.a 和 object.b 的值:" + object.a + " " + object.b); } }. 图 5-2. 5.3.2. CallByReference 运行结果. 静态变量. 程序功能:通过两个类说明静态变量/方法与实例变量/方法的区别。 程序源代码如下。 class StaticDemo { static int x; int y; public static int getX() { return x; } public static void setX(int newX) { x = newX; } public int getY() { return y; } public void setY(int newY) { y = newY; } } public class KY3_4 {. 25.

(4) 26. Java 程序设计实训指导. public static void main(String[] args) { System.out.println("静态变量 x="+StaticDemo.getX()); System.out.println("实例变量 y="+StaticDemo.getY()); // 非法,编译时将出错 StaticDemo a= new StaticDemo(); StaticDemo b= new StaticDemo(); a.setX(1); a.setY(2); b.setX(3); b.setY(4); System.out.println("静态变量 a.x="+a.getX()); System.out.println("实例变量 a.y="+a.getY()); System.out.println("静态变量 b.x="+b.getX()); System.out.println("实例变量 b.y="+b.getY()); } }. 对上面的源程序进行编译,排错并运行,结果如图 5-3 所示。. 图 5-3. 5.3.3. KY3_4 运行结果. 类的封装. 下面程序通过类中的方法来实现对私有变量的操作,补充完整并调试写出结果。 class Test1 { int a; public int b; private int c; void setc(int i) { ________; } int getc() { ________; } } class Test2 { public static void main(String args[]) { ________ object = new Test1(); object.a = 10;.

(5) 实训 5 面向对象编程练习. object.b = 20; object. ________ (100); System.out.println("a,b,and c: " + object.a + " " +object.b + " " + object. ________); } }. 5.4 拓展训练 1.编写一个矩形类 Rect,包含: 两个 protected 属性:矩形的宽 width;矩形的高 height。 两个构造器方法: (1)一个带有两个参数的构造器方法,用于将 width 和 height 属性初始化。 (2)一个不带参数的构造器方法,将矩形初始化为宽和高都为 10。 两个方法: (1)求矩形面积的方法 area()。 (2)求矩形周长的方法 perimeter()。 把程序补充完整,调试通过。 public class Rect{ protected float width, height; public Rect() { width=0; height=0; } public Rect(float width, float height) { ________=width; ________=height; } public float area() { return ________; } public float perimeter() { return ________; } } public static void main(String args[]){ Rect r1=Rect(4.5f,6.2f); System.out.println("面积为:"+r1. ________); System.out.println("周长为:"+r1. ________); }. 27.

(6) 28. Java 程序设计实训指导. 2.编写一个完整的 Java Application 程序。包含类 Student 和 TestStudent,具体要求如下: (1)Student 类: ① 属性: name:String 对象,表示姓名 sex:char 类型,表示性别 id:long 类型,表示学号 classinfo:String 对象,表示班级 address:String 对象,表示家庭地址. ② 方法: Student (String name, char sex, long id):构造函数 String getName():返回姓名 void setId(long id):设置学号 void setAddress(String add):设置家庭地址 void setClass(String classinfo):设置班级信息 public String toString():返回学生的各项信息,包括姓名、性别等上述属性. (2)类 TestStudent 作为主类要完成测试功能: ① 用以下信息生成一个 Student 对象 aGirl: 姓名: 杨阳 性别: 女 学号: 1234567 ② 设置家庭地址:浙江杭州教工路 42 号; 设置班级信息:2004 计算机 1 班。 ③ 输出对象 aGirl 的各项信息: 把下面程序补充完整,调试通过。 public class Student { String name; char sex; long id; String classinfo; String address; public Student(String name,char sex,long id){ ________= name; ________=sex; ________=id; } public String getName(){ return________; } public void setId(long id){ ________=id; }.

(7) 实训 5 面向对象编程练习. 29. public void setAddress(String add){ ________=add; } public void setClass(String classinfo){ ________=classinfo; } public String toString(){ return "姓名:"+name+" 性别:"+sex+" 学号:"+id+" 班级信息:"+classinfo+" 庭地址:"+address; } } class TestStudent{ public static void main(String[] args){ Student aGirl=new Student("杨阳",'女',1234567); aGirl. ________ ("浙江杭州教工路 42 号"); aGirl. ________ ("2004 计算机 1 班"); System.out.println(aGirl. ________); } }. 家.

(8)

參考文獻

相關文件

By using the case study and cross analysis of the results, The Purpose of this research is find out the Business implementing Supply Chain Management system project, Our study

通常把这种过去时期的,具有滞后作用的变量 叫做 滞后变量(Lagged Variable) ,含有滞后变量

This thesis makes use of analog-to-digital converter and FPGA to carry out the IF signal capture system that can be applied to a Digital Video Broadcasting - Terrestrial (DVB-T)

[16] Goto, M., Muraoka, Y., “A real-time beat tracking system for audio signals,” In Proceedings of the International Computer Music Conference, Computer Music.. and Muraoka, Y.,

Hwang, “An O (nk)-time algorithm for computing the reliability of a circular consecutive-k-out-of-n:F system,” IEEE Trans on Reliability, Vol.. Shantikumar, “Recursive algorithm

[r]

衡量一个地区或一个国家水资源的丰歉 程度的指标:

形成原因 反应物具有的总能量 生成物具有的总能量. 反应物具有的总能量