




已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java+2+程序设计实用教程(2)例3-1 Fish类的定义class Fish /关键字class用来定义类Fish String name ; /描述Fish的名字 String theColor; /描述Fish的颜色 void setValue(String n,String c ) /设置值:名字,颜色name=n;theColor=c;void swimming( ) /Fish最大的特性是能游泳 System.out.println(i can swimming.);void output( ) System.out.println(my name is:+name+ i like:+theColor);public static void main(String args) Fish clownFish = new Fish(); /创建Fish的变量clownFish clownFish.setValue(nemo,red); /调用Fish中的方法(函数)为f赋值 clownFish.output( ); /输出f所描述的鱼的信息 clownFish.swimming( ); /Fish会游泳例3-2 定义一个学生类public class StudentClssDemoString name ,dept,sex;int age,english,maths,computer;void setData(String n,String s,String d,int a, int e,int m,int c) /成员方法 name=n; age=a; /类的成员变量在整个类内有效 sex=s; dept=d; english=e; maths=m; computer=c;int total() /成员方法 int sum= english+maths+computer; /sum是方法体中的局部变量 return sum; /局部变量sum有效范围是它所在语句块void print() /成员方法 System.out.println(姓名:+name+年龄:+age+性别:+sex+系别:+dept); System.out.println(英语:+english+数学:+maths+计算机:+computer); public static void main(String argv) StudentClssDemo s=new StudentClssDemo(); /创建对象 s.setData(张楠,男,计算机系,19,80,85,90); /调用成员方法 s.print(); /成员方法的调用 System.out.println(总分:+s.total(); /成员方法的调用例3-3 Book类的定义与对象的实例化class Book String name; float price; int width, height; Book(String n,float p,int w,int h) /构造方法,用于实例化对象 name=n; price=p; width =185; height =260; void output() System.out.println(name:+name+t+price:+price); public static void main(String args) Book b1; /声明对象 b1=new Book(Java2 Programming,31.0f,185,260); /实例化对象b1 Book b2=new Book(C+ Programming,31.0f,185,260); /声明并实例化对象b2 b1.output(); /通过对象b1调用方法output() b2.output(); /通过对象b2调用方法output() 例3-4 定义一个描述书的类class BookDemo String name; float price; void output() System.out.println(name:+name+t+price:+price); public static void main(String args) BookDemo b1; /声明对象 b1=new BookDemo(); /实例化对象,数据成员被初始化为空值 b1.output(); =java2 Programming; /通过对象名为成员变量赋值 b1.price=32.5f; /通过对象名为成员变量赋值 b1.output(); 例3-5 带构造方法的类class BookDemo1 String name; float price; BookDemo1(String n,float p) /构造方法,与类同名 name=n; price=p; void output() System.out.println(name:+name+t+price:+price); public static void main(String args) BookDemo1 b1; /声明对象 b1=new BookDemo1(Java2,32.5f); /自动调用构造方法实例化对象 b1.output(); 例3-6 带访问控制权限修饰符的类class Book String name; private float price; /私有成员 Book(String n,float p) name=n; price=p; public void setPrice(float p) /公有成员,类的外部接口 price=p; public void output() /公有成员,类的外部接口 System.out.println(name:+name+t+price:+price); public class useBookDemo public static void main(String args) Book b=new Book(Java2,52.5f); b.output(); /可以访问类Book的公有成员 b.price=32.0f; /非法访问,其他类不能访问类Book的私有成员 b.setPrice(32.0f); /正确的访问 b.output(); 例3-7 带静态成员变量的类class useBook String name; float price; int count=0; /记录创建对象的个数 static int bookNumber=100; /静态变量,书的起始编号为100 useBook(String n,float p) name=n; price=p; count+; /记录创建对象个数,增加1 bookNumber+; /书号实现自动增加1 void output() prt(name:+name+t+price:+price+t); prt(bookNumber:+bookNumber+t+count:+count+n); public static void prt(String s) System.out.print(s); public static void main(String args) useBook b1,b2,b3; /声明对象 b1=new useBook(Java2,36.0f); b1.output(); b2=new useBook(C+,40.0f); b2.output(); b3=new useBook(VB ,35.0f); b3.output(); 例3-8 静态变量的访问/*class Book String name; private float price; Book(String n,float p) name=n; price=p; public void setPrice(float p) /类的外部接口 price=p; public void output() /类的外部接口 System.out.println(name:+name+t+price:+price); public class useBook public static void main(String args) Book b=new Book(Java2,32.5f); /自动调用构造方法实例化对象 b.output(); b.price=100.0f; /非法访问 b.output(); */*class useBook String name; float price; int count=0; /记录创建对象的个数 static int bookNumber=100; /静态变量,书的起始编号为100 useBook(String n,float p) name=n; price=p; count+; /记录创建对象个数,增加1 bookNumber+; /书号实现自动增加1 void output() prt(name:+name+t+price:+price+t); prt(bookNumber:+bookNumber+t+count:+count+n); public static void prt(String s) System.out.print(s); public static void main(String args) useBook b1,b2,b3; /声明对象 b1=new useBook(Java2,36.0f); b1.output(); b2=new useBook(C+,40.0f); b2.output(); b3=new useBook(VB ,35.0f); b3.output(); */public class Circle static double PI=3.1415926; int radius; Circle(int r ) radius=r; double getArea( ) double area=radius*radius*PI;/创建对象后可以直接访问,也可以通过类名访问 return area; public static void main(String args) System.out.println(圆周率的值: +Circle.PI);/创建实例前,可访问 Circle c=new Circle(10); System.out.println(c.getArea(); 例3-9 带静态方法的类class Book String name; private float price; static int count; /静态变量,统计对象个数 Book(String n,float p) name=n; price=p; count+; /加1 public static void disCount() /静态方法 System.out.print(Count:+count+t); /只能访问静态成员 public void output() /实例方法 disCount(); /实例方法可以访问静态成员 System.out.println(name:+name+t+price:+price); public class useBookDemo2 public static void main(String args) Book.disCount(); /通过类名调用静态方法 System.out.print(n); /换行 Book b1=new Book(Java2,32.5f); b1.output(); Book b2=new Book(C+,32.5f); b2.output(); Book b3=new Book(Delphi,32.5f); b3.output(); Book.disCount(); /通过类名调用静态方法 例3-10 传值传递基本数据类型参数public class PassValue static void exchange(int a,int b) /静态方法才被main方法调用 int temp; temp=a; a=b; b=temp; public static void main(String args) int i=10; int j=100; System.out.println(before call:+i=+i+tj=+j); /调用前 exchange(i,j); System.out.println(after call:+i=+i+tj=+j); /调用后 例3-11 引用传递对象作为参数class Book String name; private float price; Book(String n, float p) /构造方法 name=n; price=p; static void change(Book a_book, String n, float p) /静态方法,对象作为参数 a_=n; a_book.price=p; public void output() /实例方法,输出对象信息 System.out.println(name:+name+t+price:+price); public class PassAddr public static void main(String args) Book b=new Book(Java2,32.5f); System.out.print(before call:t); /调用前 b.output(); b.change(b,C+,45.5f); /引用传递,对象b作为参数 System.out.print(after call:t); /调用后 b.output(); 例4-1 Object类常用方法class Book_Obj String name; private int price; Book_Obj(String n,int p) name=n; price=p; public static void prt(String s) System.out.println(s); public static void main(String args) String s1=new String(123456); String s2=new String(123456); prt(s2 equals s1:t+s2.equals(s1); /字符串对象s1和s2含有一样的信息 Book_Obj b1=new Book_Obj(Java2,40); Book_Obj b2=new Book_Obj(Java2,40); Book_Obj b=b1; /为对象b赋值 prt(b2.equals b1:t+b2.equals(b1); /不是一个对象实体返回false prt(b.equals b1:t+b.equals(b1); /指向同一个对象实体返回true prt(hashCode of b1:t+b1.hashCode(); prt(hashCode of b2:t+b2.hashCode(); prt(call toString() by b1:t+b1.toString(); prt(b2 auto call toString():t+b2); prt(Get the class name of b1:t+b1.getClass( ).getName(); 例4-2 子类继承超类成员class Animal String name; /默认成员变量 int age;protected int weight; /保护成员变量 图 4 2 Fish的运行结果 protected void setData(String n,int a,int w) /保护成员方法 name = n; age = a; weight=w; public void ShowInfo() System.out.println(name +t+ age +t+ weight); public class FishInherit extends Animal int SwimSpeed; public static void main(String args) Animal a = new Animal() ; a.setData(Shred,26,90); a.ShowInfo(); FishInherit f = new FishInherit(); f.setData(Nemo,1,6); /子类对象调用继承的方法初始化对象 f.SwimSpeed=76; /为新添加的成员变量赋值 f.ShowInfo( ); /子类对象调用继承的方法输出信息 例4-3 成员方法的重载public class ex_add public static int add(int x,int y) int sum =x+y; return sum; public static int add(int x,int y,int z) int sum =x+y+z; return sum; public static void main(String args) int i=10,j=20,k=30; System.out.println(i=+i+tj=+j+ +ti+j=+add(i,j); System.out.println(i=+i+tj=+j+tk=+k+ti+j+k=+add(i,j,k); 例4-4 构造方法的重载public class Book_cons static int no=1000; /图书编号,类成员变量 String name; private float price; Book_cons(String name, float price) =name; /this引用指名成员变量 this.price=price; no+; Book_cons(String name) /构造方法重载 this(name,0.0f); /调用同类的构造函数 Book_cons(float price) /构造方法重载 this(不知书名,price); Book_cons() /构造方法重载 this(不知书名,0.0f); public void output() /实例方法 System.out.print(书号t+no); System.out.println(t书名: +name+t价格: +price); public void output(String p) /成员方法重载 System.out.print(书号t+no); System.out.println(t书名: +name+t价格: +p); public static void main(String args) String p=不知价格; Book_cons b1=new Book_cons(Java2,32.5f); b1.output(); Book_cons b2=new Book_cons(C+); b2.output(p); Book_cons b3=new Book_cons(45.6f); b3.output(); Book_cons b4=new Book_cons(); b4.output(p); 例4-5 方法的覆盖class Animal protected String name; protected int age; public Animal(String n,int a) = n; this.age = a; public void ShowInfo() System.out.print(n+this.getClass().getName()+t); System.out.print(name +t+ age +t); public class FishDemo extends Animal int SwimSpeed; FishDemo(String n,int a,int s) super(n, a); /调用超类的构造方法 SwimSpeed=s; public void ShowInfo() /方法覆盖,增加新的功能 super.ShowInfo(); /调用超类的实例方法 System.out.println(SwimSpeed ); public static void main(String args) Animal a=new Animal(Shred,26); a.ShowInfo(); FishDemo f=new FishDemo(Nemo,1,6); f.SwimSpeed=76; f.ShowInfo(); 例4-6 超类可以引用子类对象class Animal protected String name; public Animal(String n) name = n; public void ShowInfo() System.out.print(the animalt); System.out.print(name +t); public void cry() System.out.print(Crying.n); class Fish extends Animal int SwimSpeed; Fish(String n, int s) super(n); /调用超类的构造方法 SwimSpeed=s; public void ShowInfo( ) /方法覆盖 System.out.print(the Fisht+name +t); System.out.print(Swimming speed:t+ SwimSpeed); public void cry() /方法覆盖 System.out.print(tBubbing.n); class Bird extends Animal int FlySpeed; Bird(String n,int f) super(n); /调用超类的构造方法 FlySpeed=f; public void ShowInfo( ) /方法覆盖 System.out.print(the Birdt+name +t); System.out.print(Flying speed:t+ FlySpeed); public void cry() /方法覆盖 System.out.print(tSinging.n); public class InheritDemo4_6 public static void main(String args) Animal a; a=new Animal(only a Animal); /超类对象引用子类创建的对象 a.ShowInfo(); /调用的是子类的覆盖后的方法 a.cry(); /调用的是子类的覆盖后的方法 a=(Animal)new Fish(Nemo ,20); a.ShowInfo(); a.cry(); Bird b=new Bird(Eagle ,80); a=b; /把一个子类对象直接赋值给超类对象 a.ShowInfo(); a.cry(); 例4-7 子类构造函数的定义class Animal /超类没显式定义构造函数 protected String name; public void setData(String n) name = n; public void ShowInfo() System.out.print(the animalt); System.out.print(name +t); public void cry() System.out.print(Crying.n); class Fish extends Animal int SwimSpeed; Fish(int s) /隐式调用超类的默认构造函数,将name设置为默认值null SwimSpeed=s; Fish(String n,int s) name=n; SwimSpeed=s; public void ShowInfo( ) /方法覆盖 System.out.print(the Fisht+name +t); System.out.print(Swimming speed:t+ SwimSpeed); public void cry() System.out.print(tBubbing.n); public class InheritDemo4_7 public static void main(String args) Fish a=new Fish(Nemo,60); a.ShowInfo(); a.cry(); Fish b=new Fish(60); /继承的成员name被设置为null b.ShowInfo(); b.cry(); 例4-8 抽象类的应用abstract class Animal /抽象类 protected String name; public abstract void move(); /没有方法体的抽象方法 public abstract void cry(); /没有方法体的抽象方法 public void ShowInfo(String n) System.out.print(n+n+t); System.out.print(name +t); class Fish extends Animal int SwimSpeed; Fish(String n,int s) name=n; SwimSpeed=s; public void move() /子类必须覆盖抽象类的抽象方法,鱼的走是游泳 System.out.print( swimming speed:t +SwimSpeed ); public void cry() /子类必须覆盖抽象类的抽象方法 System.out.print(tsinging.t ); class Bird extends Animal int FlySpeed; Bird(String n,int f) name=n; FlySpeed=f; public void move() /子类必须覆盖抽象类的抽象方法,鸟的走是飞翔 System.out.print( Flying speed:t +FlySpeed); public void cry() /子类必须覆盖抽象类的抽象方法 System.out.print(tbubbing.t ); class Snake extends Animal int CrawlSpeed; Snake(String n,int c) name=n; CrawlSpeed=c; public void move() /子类必须覆盖抽象类的抽象方法,蛇的走是爬行 System.out.print( Crawling speed:t +CrawlSpeed); public void cry() /子类必须覆盖抽象类的抽象方法 System.out.print(tchichi.t ); public class Animal_abstract public static void main(String args) Fish f=new Fish(Shark,80); f.ShowInfo(Fish); f.move(); f.cry(); Bird b=new Bird(Eagle,160); b.ShowInfo(Bird); b.move(); b.cry(); Snake s=new Snake(Nemo,20); s.ShowInfo(Cobra); s.move(); s.cry(); 例5-1 接口的实现interface theShape double PI=3.14159; double getArea();interface showShape void showInfor();public class Circle_inter implements theShape,showShape int r; Circle_inter(int r) this.r=r; public double getArea() /必须声明为public return r*r*PI; public void showInfor() /必须声明为public System.out.print(r= +r+tthe area: +getArea(); public static void main(String args) Circle_inter c=new Circle_inter(10); c. showInfor(); 例5-2 接口的应用interface access /接口的定义 void push(char e); char pop();class aStack implements access /一个堆栈类 private char element=new char50; private int top=0; public void push(char e) /实现接口的方法,压栈操作 elementtop+=e; public char pop() /实现接口的方法,出栈操作 return element-top; class aQueue implements access /一个队列类 private char element=new char50; private int front=0; private int rear=0; public void push(char e) /实现接口的方法,入队操作 elementrear+=e; public char pop() /实现接口的方法,出队操作 return elementfront+; public class useInterfaceDemo public static void showElement(char e) /输出一个字符 System.out.print(t+e); public static void main(String args) aStack s=new aStack(); s.push(A); s.push(B); s.push(1); s.push(2); System.out.println(The operation of Stack:); showElement(s.pop(); showElement(s.pop(); showElement(s.pop(); showElement(s.pop(); System.out.println(); aQueue q=new aQueue(); q.push(A); q.push(B); q.push(1); q.push(2); System.out.println(The operation of Queue:); showElement(q.pop(); showElement(q.pop(); showElement(q.pop(); showElement(q.pop(); 例5-3 内部类的定义public class InterclassDemo /外部类 class GetYear /内部类 private int i = 2008; pu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 广播电视与通信课件
- 安全培训效益评估方案课件
- 2025年浙江杭州市萧山区第三人民医院招聘编外人员1人考前自测高频考点模拟试题及答案详解(有一套)
- Hydroxyethyl-starch-Mw-110-150-kDa-生命科学试剂-MCE
- 2025年精密箱体系统项目合作计划书
- hCA-I-hCA-II-IN-1-生命科学试剂-MCE
- 2025年重水堆核电站及配套产品项目发展计划
- 2025广西来宾盛亿土地整治开发有限公司招聘拟聘人员模拟试卷及答案详解(历年真题)
- 2025年延安通和电业有限责任公司招聘(5人)模拟试卷及答案详解(夺冠)
- 技术方案编制与评审工具
- 《情满今生》读书笔记模板
- 胸痛中心网络医院STEMI患者绕行急诊和CCU方案流程图
- 2021年一级注册消防工程师继续教育试题答案
- 急危重病人营养与代谢支持
- 甲醇理化性质及危险特性表MSDS
- GB/T 7216-2009灰铸铁金相检验
- GB/T 5796.3-1986梯形螺纹基本尺寸
- 华北理工大学2016年《互换性及技术测量》期末考试复习题
- 医学影像学总论-X线课件
- 大班科学《神奇的洞洞》课件
- 第二次全国陆生野生动物资源调查技术规程
评论
0/150
提交评论