第四章类、对象和接口_第1页
第四章类、对象和接口_第2页
第四章类、对象和接口_第3页
第四章类、对象和接口_第4页
第四章类、对象和接口_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第四章 类、对象和接口例子1class XiyoujiRenwu float height,weight; String head, ear,hand,foot, mouth; void speak(String s) System.out.println(s); class A public static void main(String args) XiyoujiRenwu zhubajie; /声明对象 zhubajie=new XiyoujiRenwu(); /为对象分配内存,使用new 运算符和默认的构造方法 例子2class Point int x,y; Point(int a,i

2、nt b) x=a; y=b; public class A public static void main(String args) Point p1,p2; /声明对象p1和p2 p1=new Point(10,10); /为对象分配内存,使用 new 和类中的构造方法 p2=new Point(23,35); /为对象分配内存,使用 new 和类中的构造方法例子3class XiyoujiRenwu float height,weight; String head, ear,hand,foot,mouth;void speak(String s) head="歪着头"

3、 System.out.println(s); class Example4_3 public static void main(String args) XiyoujiRenwu zhubajie,sunwukong;/声明对象 zhubajie=new XiyoujiRenwu(); /为对象分配内存 sunwukong=new XiyoujiRenwu(); zhubajie.height=1.80f; /对象给自己的变量赋值 zhubajie.head="大头" zhubajie.ear="一双大耳朵" sunwukong.height=1.62

4、f; /对象给自己的变量赋值 sunwukong.weight=1000f; sunwukong.head="绣发飘飘" System.out.println("zhubajie的身高:"+zhubajie.height); System.out.println("zhubajie的头:"+zhubajie.head); System.out.println("sunwukong的重量:"+sunwukong.weight); System.out.println("sunwukong的头:"+

5、sunwukong.head); zhubajie.speak("俺老猪我想娶媳妇"); /对象调用方法 System.out.println("zhubajie现在的头:"+zhubajie.head); sunwukong.speak("老孙我重1000斤,我想骗八戒背我"); /对象调用方法 System.out.println("sunwukong现在的头:"+sunwukong.head); 例子4class 梯形 float 上底,下底,高,面积; 梯形(float x,float y,float h)

6、 上底=x; 下底=y; 高=h; float 计算面积() 面积=(上底+下底)*高/2.0f; return 面积; void 修改高(float height) 高=height; float 获取高() return 高; public class Example4_4 public static void main(String args) 梯形 laderOne=new 梯形(12.0f,3.5f,50),laderTwo=new 梯形(2.67f,3.0f,10); System.out.println("laderOne的高是:"+laderOne.获取高(

7、); System.out.println("laderTwo的高是:"+laderTwo.获取高(); System.out.println("laderOne的面积是:"+laderOne.计算面积(); System.out.println("laderTwo的面积是:"+laderTwo.计算面积(); laderOne.修改高(10); float h=laderOne.获取高(); laderTwo.修改高(h*2); System.out.println("laderOne现在的高是:"+laderO

8、ne.获取高(); System.out.println("laderTwo现在的高是:"+laderTwo.获取高(); System.out.println("laderOne现在的面积是:"+laderOne.计算面积(); System.out.println("laderTwo现在的面积是:"+laderTwo.计算面积(); 例子5class People String face; void setFace(String s) face=s; class A void f(int x,double y,People p)

9、x=x+1; y=y+1; p.setFace("笑脸"); System.out.println("参数x和y的值分别是:"+x+","+y); System.out.println("参数对象p的face是:"+p.face); public class Example4_5 public static void main(String args) int x=100; double y=100.88; People zhang=new People(); zhang.setFace("很严肃的样子&

10、quot;); A a=new A(); a.f(x,y,zhang); System.out.println("main方法中x和y的值仍然分别是:"+x+","+y); System.out.println("main方法中对象zhang的face是:"+zhang.face); 例子6class 圆 double 半径; 圆(double r) 半径=r; double 计算面积() return 3.14*半径*半径; void 修改半径(double 新半径) 半径=新半径; double 获取半径() return 半径;

11、 class 圆锥 圆 底圆; double 高; 圆锥(圆 circle,double h) this.底圆=circle; this.高=h; double 计算体积() double volume; volume=底圆.计算面积()*高/3.0; return volume; void 修改底圆半径(double r) 底圆.修改半径(r); double 获取底圆半径() return 底圆.获取半径(); class Example4_6 public static void main(String args) 圆 circle=new 圆(10); 圆锥 circular=new

12、圆锥(circle,20); System.out.println("圆锥底圆半径:"+circular.获取底圆半径(); System.out.println("圆锥的体积:"+circular.计算体积(); circular.修改底圆半径(100); System.out.println("圆锥底圆半径:"+circular.获取底圆半径(); System.out.println("圆锥的体积:"+circular.计算体积(); 例子7class 梯形 float 上底,高; static float

13、下底;梯形(float x,float y,float h) 上底=x; 下底=y; 高=h; float 获取下底() return 下底; void 修改下底(float b) 下底=b; class Example4_7 public static void main(String args) 梯形 laderOne=new 梯形(3.0f,10.0f,20),laderTwo=new 梯形(2.0f,3.0f,10); 梯形.下底=200; /通过类名操作类变量 System.out.println("laderOne的下底:"+laderOne.获取下底(); S

14、ystem.out.println("laderTwo的下底:"+laderTwo.获取下底(); laderTwo.修改下底(60); /通过对象操作类变量 System.out.println("laderOne的下底:"+laderOne.获取下底(); System.out.println("laderTwo的下底:"+laderTwo.获取下底(); 例子8class Fibi public static long fibinacii(int n) long c=0; if(n=1|n=2) c=1; else c=fibi

15、nacii(n-1)+fibinacii(n-2); return c; public class Example4_8 public static void main(String args) System.out.println(Fibi.fibinacii(7); 例子9package tom.jiafei;public class PrimNumber public void getPrimnumber(int n) int sum=0,i,j; for(i=1;i<=n;i+) for(j=2;j<=i/2;j+) if(i%j=0) break; if(j>i/2

16、) System.out.print(" "+i); public static void main(String args) PrimNumber p=new PrimNumber(); p.getPrimnumber(20); 例子10import java.applet.Applet;import java.awt.*;public class Example4_10 extends Applet Button redbutton; public void init() redbutton=new Button("我是一个红色的按钮"); redb

17、utton.setBackground(Color.red);redbutton.setForeground(Color.white); add(redbutton); 例子11import tom.jiafei.*; /引入包tom.jiafei中的类public class Example4_11 public static void main(String args) PrimNumber num=new PrimNumber();/用包tom.jiafei中的类创建对象 num.getPrimnumber(30); 例子12public class Example4_12 public

18、 static void main(String args) PrimNumber num=new PrimNumber();/要保证PrimNuber类和Example4_12类在同一目录中 num.getPrimnumber(120); Trangel.javapackage tom.jiafei;public class Trangle double sideA,sideB,sideC; boolean boo; public Trangle(double a,double b,double c) sideA=a; sideB=b; sideC=c; if(a+b>c&&a

19、mp;a+c>b&&c+b>a) System.out.println("我是一个三角形"); boo=true; else System.out.println("我不是一个三角形"); boo=false; public void 计算面积() if(boo) double p=(sideA+sideB+sideC)/2.0; double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC) ; System.out.println("面积是:"+area);

20、else System.out.println("不是一个三角形,不能计算面积"); public void 修改三边(double a,double b,double c) sideA=a; sideB=b; sideC=c; if(a+b>c&&a+c>b&&c+b>a) boo=true; else boo=false; 例子13import tom.jiafei.Trangle;class Example4_13 public static void main(String args) Trangle trangle

21、=new Trangle(12,3,1); trangle.计算面积(); trangle.修改三边(3,4,5); trangle.计算面积(); 例子14class Example4_14 private int money;Example4_14() money=2000; private int getMoney() return money; public static void main(String args) Example4_14 exa=new Example4_14();exa.money=3000;int m=exa.getMoney(); System.out.pri

22、ntln("money="+m); 例子15class Father private int money; float weight,height; String head; void speak(String s) System.out.println(s); class Son extends Father String hand,foot; public class Example4_15 public static void main(String args) Son boy; boy=new Son(); boy.weight=1.80f; boy.height=

23、120f; boy.head="一个头" boy.hand="两只手 " boy.foot="两只脚" boy.speak("我是儿子"); System.out.println(boy.hand+boy.foot+boy.head+boy.weight+boy.height); 例子16class Chengji float f(float x,float y) return x*y; class Xiangjia extends Chengji float f(float x,float y) return x

24、+y ; public class Example4_16 public static void main(String args) Xiangjia sum; sum=new Xiangjia(); float c=sum.f(4,6); System.out.println(c); 例子17class Area float f(float r ) return 3.14159f*r*r; float g(float x,float y) return x+y; class Circle extends Area float f(float r) return 3.14159f*2.0f*r

25、; public class Example4_17 public static void main(String args) Circle yuan; yuan=new Circle(); float length=yuan.f(5.0f); float sum=yuan.g(232.645f,418.567f); System.out.println(length); System.out.println(sum); 例子18class A final double PI=3.1415926; public double getArea(final double r) return PI*

26、r*r; public class Example4_18 public static void main(String args) A a=new A(); System.out.println("面积:"+a.getArea(100); 例子19class 类人猿 private int n=100; void crySpeak(String s) System.out.println(s); class People extends 类人猿 void computer(int a,int b) int c=a*b; System.out.println(c); voi

27、d crySpeak(String s) System.out.println("*"+s+"*"); class Example4_19 public static void main(String args) 类人猿 monkey=new People(); monkey.crySpeak("I love this game");People people=(People)monkey; puter(10,10); 例子20class 动物 void cry() class 狗 extends 动物 void cry() Syst

28、em.out.println("汪汪."); class 猫 extends 动物 void cry() System.out.println("喵喵."); class Example4_20 public static void main(String args) 动物 dongwu; dongwu=new 狗(); dongwu.cry(); dongwu=new 猫(); dongwu.cry(); 例子21abstract class A abstract int min(int x,int y); int max(int x,int y) r

29、eturn x>y?x:y; class B extends A int min(int x,int y) return x<y?x:y; public class Example4_21 public static void main(String args) A a; B b=new B(); int max=b.max(12,34); int min=b.min(12,34); System.out.println("max="+max+" min="+min); a=b; max=a.max(12,34); System.out.pr

30、intln("max="+max); 例子22abstract class 图形 public abstract double 求面积();class 梯形 extends 图形 double a,b,h; 梯形(double a,double b,double h) this.a=a;this.b=b;this.h=h; public double 求面积() return(1/2.0)*(a+b)*h); class 圆形 extends 图形 double r; 圆形(double r) this.r=r; public double 求面积() return(3.1

31、4*r*r); class 堆 图形 底; double 高; 堆(图形 底,double 高) this.底=底; this.高=高; void 换底(图形 底) this.底=底; public double 求体积() return (底.求面积()*高)/3.0; public class Example4_22 public static void main(String args) 堆 zui; 图形 tuxing; tuxing=new 梯形(2.0,7.0,10.7); System.out.println("梯形的面积"+tuxing.求面积(); zui

32、=new 堆(tuxing,30); System.out.println("梯形底的堆的体积"+zui.求体积(); tuxing=new 圆形(10); System.out.println("半径是10的圆的面积"+tuxing.求面积(); zui.换底(tuxing); System.out.println("圆形底的堆的体积"+zui.求体积(); 例子23class Student int number;String name;Student() Student(int number,String name) this.

33、number=number;=name; System.out.println("I am "+name+ "my number is "+number); class Univer_Student extends Student boolean 婚否; Univer_Student(int number,String name,boolean b) super(number,name); 婚否=b; System.out.println("婚否="+婚否); public class Example4_23 pub

34、lic static void main(String args) Univer_Student zhang=new Univer_Student(9901,"和晓林",false); 例子24class Sum int n; float f() float sum=0; for(int i=1;i<=n;i+) sum=sum+i; return sum; class Average extends Sum int n; float f() float c; super.n=n; c=super.f(); return c/n; float g() float c;

35、 c=super.f(); return c/2; public class Example4_24 public static void main(String args) Average aver=new Average(); aver.n=100; float result_1=aver.f(); float result_2=aver.g(); System.out.println("result_1="+result_1); System.out.println("result_2="+result_2); 例子25interface Comp

36、utable int MAX=100; int f(int x);class China implements Computable int number; public int f(int x) /不要忘记public关键字 int sum=0; for(int i=1;i<=x;i+) sum=sum+i; return sum; class Japan implements Computable int number; public int f(int x) return 44+x; public class Example4_25 public static void main(

37、String args) China zhang; Japan henlu; zhang=new China(); henlu=new Japan(); zhang.number=991898+Computable.MAX; henlu.number=941448+Computable.MAX; System.out.println("number:"+zhang.number+"求和"+zhang.f(100); System.out.println("number:"+henlu.number+"求和"+hen

38、lu.f(100); 例子26interface 收费 public void 收取费用();interface 调节温度 public void controlTemperature();class 公共汽车 implements 收费 public void 收取费用() System.out.println("公共汽车:一元/张,不计算公里数"); class 出租车 implements 收费, 调节温度 public void 收取费用() System.out.println("出租车:1.60元/公里,起价3公里"); public voi

39、d controlTemperature() System.out.println("安装了Hair空调"); class 电影院 implements 收费,调节温度 public void 收取费用() System.out.println("电影院:门票,十元/张"); public void controlTemperature() System.out.println("安装了中央空调"); class Example4_26 public static void main(String args) 公共汽车 七路=new

40、公共汽车(); 出租车 天宇=new 出租车(); 电影院 红星=new 电影院(); 七路.收取费用(); 天宇.收取费用(); 红星.收取费用(); 天宇.controlTemperature(); 红星.controlTemperature(); 例子27interface ShowMessage void 显示商标(String s);class TV implements ShowMessage public void 显示商标(String s) System.out.println(s); class PC implements ShowMessage public void 显

41、示商标(String s) System.out.println(s); public class Example4_27 public static void main(String args) ShowMessage sm; sm=new TV(); sm.显示商标("长城牌电视机"); sm=new PC(); sm.显示商标("联想奔月5008PC机"); 例子28interface Computerable public double 求面积();class 梯形 implements Computerable double a,b,h;梯形(

42、double a,double b,double h) this.a=a;this.b=b;this.h=h; public double 求面积() return(1/2.0)*(a+b)*h); class 圆形 implements Computerable double r; 圆形(double r) this.r=r; public double 求面积() return(3.14*r*r); class 堆 Computerable 底; double 高; 堆(Computerable 底,double 高) this.底=底; this.高=高; void 换底(Compute

43、rable 底) this.底=底; public double 求体积() return (底.求面积()*高)/3.0; public class Example4_28 public static void main(String args) 堆 zui; Computerable bottom; bottom=new 梯形(2.0,7.0,10.7); System.out.println("梯形的面积"+bottom.求面积(); zui=new 堆(bottom,30); System.out.println("梯形底的堆的体积"+zui.求

44、体积(); bottom=new 圆形(10); System.out.println("半径是10的圆的面积"+bottom.求面积(); zui.换底(bottom); System.out.println("圆形底的堆的体积"+zui.求体积(); 例子29interface SpeakHello void speakHello();class Chinese implements SpeakHello public void speakHello() System.out.println("中国人习惯问候语:你好,吃饭了吗? "

45、;); class English implements SpeakHello public void speakHello() System.out.println("英国人习惯问候语:你好,天气不错 "); class KindHello public void lookHello(SpeakHello hello) hello.speakHello(); public class Example4_29 public static void main(String args) KindHello kindHello=new KindHello(); kindHello

46、.lookHello(new Chinese(); kindHello.lookHello(new English(); 例子30class China final String nationalAnthem="义勇军进行曲" Beijing beijing; China() beijing=new Beijing(); String getSong() return nationalAnthem; class Beijing String name="北京" void speak() System.out.println("我们是"

47、+name+" 我们的国歌是:"+getSong(); public class Example4_30 public static void main(String args) China china=new China(); china.beijing.speak(); 例子31class Cubic double getCubic(int n) return 0; abstract class Sqrt public abstract double getSqrt(int x);class A void f(Cubic cubic) double result=cubic.getCubic(3); S

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论