收藏 分销(赏)

实验六-类和对象.doc

上传人:仙人****88 文档编号:9280396 上传时间:2025-03-19 格式:DOC 页数:5 大小:65.50KB
下载 相关 举报
实验六-类和对象.doc_第1页
第1页 / 共5页
实验六-类和对象.doc_第2页
第2页 / 共5页
点击查看更多>>
资源描述
实验六 类和对象(二) 一、实验目的 掌握类的定义、对象的创建与初始化、变量作用域及值传递。 二、实验要求 1、熟练掌握类的定义,构造方法的使用和对象的创建。 2、掌握方法的重载。 3、掌握变量的不同作用域及重载下变量值的传递。 4、分析程序的运行结果并记录所编写的程序、执行结果以及出错时的处理过程。 三、实验内容 参考书上示例及下面参考程序完成实验任务。 四、参考程序 1、编写一个userName类,包含两个成员变量:firstname,lastname;构建两个构造方法,一个是带参数,一个是不带参数,将firstname和lastname进行初始化;最后构建一个fullName方法,用来返回全名。 2、变量值传递 class ValueTransfer { void modify(int i){ i++; } void modify(int[] arr){ for(int i=0;i<arr.length;i++){ arr[i]=1; } } void modify(SimpleClass s){ s.field=1; } public static void main(String[] args) { ValueTransfer v=new ValueTransfer(); int intValue=0; v.modify(intValue); System.out.println("intValue="+intValue); int[] intArr=new int[1]; intArr[0]=100; v.modify(intArr); System.out.println("intArr[0]="+intArr[0]); SimpleClass s=new SimpleClass(); v.modify(s); System.out.println("s.field="+s.field); } } class SimpleClass{ int field; } 运行结果为: intValue=0 intArr[0]=1 s.field=1 五、实验任务 1. 什么是构造方法?它的作用是什么?怎么调用? 2. 什么叫方法重载? 3. 完成课后实验三。 class Employee{ private String name; private double salary; public Employee(String n,double s){ this.name=n; this.salary=s; } public String getName(){ return this.name; } public double getSalary(){ return this.salary; } public void raiseSalary(double p){ double raise=salary*p/100; salary=salary+raise; } public static void main(String[] args){ Employee ee[]=new Employee[3]; ee[0]=new Employee("张三",1000); ee[1]=new Employee("李四",2000); ee[2]=new Employee("王五",1800); for(int i=0;i<ee.length;i++){ ee[i].raiseSalary(7); System.out.println("名字="+ee[i].getName()+" 薪水="+ee[i].getSalary()); } } } 运行结果为: 名字=张三 薪水=1070.0 名字=李四 薪水=2140.0 名字=王五 薪水=1926.0 4. 公司给员工发工资,定义一个员工类,使用构造方法的重载,一般员工的2000,经理4000,董事长8000,一般员工发工资使用默认的构造方法,经理和董事长发工资使用带参数的。最后打印出不同职位的工资。 public class Employee{ private String job; private double sal; public Employee(){ job = "普通员工"; sal = 2000; } public Employee(String job, double sal){ this.job = job; this.sal = sal; } public String toString(){ return this.job + "\t工资为:" + this.sal; } public static void main(String [] args){ Employee e1 = new Employee(); Employee e2 = new Employee("经理", 4000); Employee e3 = new Employee("董事长", 8000); System.out.println(e1); System.out.println(e2); System.out.println(e3); } } 运行结果为: 普通员工 工资为:2000.0 经理 工资为:4000.0 董事长 工资为:8000.0 5. 编写一个包含不同作用域变量的例子,体会不同作用域的具体含义。 6.编写一个复数类,具有实、虚部成员变量,可以完成加、减、乘、获得实部和虚部等操作,并编写一个主类测试复数类的运算。 class ImaginNo{ private double a;//实部 private double b;//虚部 public void set(double a,double b){ //设置当前复数的值 this.a=a; this.b=b; } public double getA(){ //获取实部 return this.a; } public double getB(){ //获取虚部 return this.b; } public void add(double a,double b){ //加法 this.a+=a; this.b+=b; } public void minus(double a,double b){ //减法 this.a-=a; this.b-=b; } public void multiply(double a,double b){ //乘法 this.a=this.a*a-this.b*b; this.b=this.a+a; } public void divide(double a,double b){ //除法 if(a*b!=0){ this.a=(this.a*a+this.b*b)/(a*a+b*b); this.b=(this.b*a-this.a*b)/(a*a+b*b); } else{ System.out.println("除数不能为0"); } } public void print(){ System.out.println("z= "+this.a+"+("+this.b+")i"); } } public class ImNum { public static void main(String[] args) { ImaginNo z = new ImaginNo(); z.set(2, 5); z.add(-1, 2); z.print(); z.minus(2, 3); z.print(); z.multiply(3, 2); z.print(); z.divide(4, 2); z.print(); System.out.println("z的实部为:"+z.getA()); System.out.println("z的虚部为:"+z.getB()); } } 运行结果为: z= 1.0+(7.0)i z= -1.0+(4.0)i z= -11.0+(-8.0)i z= -3.0+(-1.3)i z的实部为:-3.0 z的虚部为:-1.3
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服