




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
类、对象和接口程序代码例子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,int 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=歪着头; 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.62f; /对象给自己的变量赋值 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的头:+sunwukong.head); zhubajie.speak(俺老猪我想娶媳妇); /对象调用方法 System.out.println(zhubajie现在的头:+zhubajie.head); sunwukong.speak(老孙我重1000斤,我想骗八戒背我); /对象调用方法 System.out.println(sunwukong现在的头:+sunwukong.head); 例子7class 梯形 float 上底,高; static float 下底;梯形(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.获取下底(); System.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=fibinacii(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;ji/2) System.out.print( +i); public static void main(String args) PrimNumber p=new PrimNumber(); p.getPrimnumber(20); 例子10 Applet小应用程序的例子例子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 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+bc&a+cb&c+ba) 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); else System.out.println(不是一个三角形,不能计算面积); public void 修改三边(double a,double b,double c) sideA=a; sideB=b; sideC=c; if(a+bc&a+cb&c+ba) boo=true; else boo=false; 例子13import tom.jiafei.Trangle;class Example4_13 public static void main(String args) Trangle trangle=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.println(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=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+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; 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*r*r; public class Example4_18 public static void main(String args) A a=new A(); System.out.println(面积:+a.getArea(100); 例子20class 动物 void cry() class 狗 extends 动物 void cry() System.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(); 例子23class Student int number;String name;Student() Student(int number,String name) this.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 public 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; 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 Computable 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(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+求和+henlu.f(100); 例子26interface 收费 public void 收取费用();interface 调节温度 public void controlTemperature();class 公共汽车 implements 收费 public void 收取费用() System.out.println(公共汽车:一元/张,不计算公里数); class 出租车 implements 收费, 调节温度 public void 收取费用() System.out.println(出租车:1
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《猫》小学教案七篇
- 产品返利销售合同范本
- 代理记账软件合同范本
- 养护设计合同范本
- hr签订无效合同范本
- 厨具拆除服务合同范本
- 农村搬迁赔偿合同范本
- 区块链支付合同范例
- 口腔诊所医生合同范本
- 119消防宣传日活动总结
- 新版人教版七年级下册数学全册教案教学设计含教学反思
- 《中国古代寓言》导读(课件)2023-2024学年统编版语文三年级下册
- 小学梦想开《去远方》教学设计
- Q∕SY 06349-2019 油气输送管道线路工程施工技术规范
- CEO自恋及其经济后果研究:以格力电器为例
- 六鑫伺服刀塔说明书LS系列
- 19.骨折术后内固定取出临床路径
- 水利水电工程金属结构与机电设备安装安全技术规程
- 肾内科临床诊疗规范(南方医院)
- 珍爱生命 安全第一 中小学主题教育班会
- 二十八星宿(课堂PPT)
评论
0/150
提交评论