第四Java面向对象编程基础.ppt

上传人:sccc 文档编号:4746482 上传时间:2023-05-13 格式:PPT 页数:98 大小:637.52KB
返回 下载 相关 举报
第四Java面向对象编程基础.ppt_第1页
第1页 / 共98页
第四Java面向对象编程基础.ppt_第2页
第2页 / 共98页
第四Java面向对象编程基础.ppt_第3页
第3页 / 共98页
第四Java面向对象编程基础.ppt_第4页
第4页 / 共98页
第四Java面向对象编程基础.ppt_第5页
第5页 / 共98页
点击查看更多>>
资源描述

《第四Java面向对象编程基础.ppt》由会员分享,可在线阅读,更多相关《第四Java面向对象编程基础.ppt(98页珍藏版)》请在三一办公上搜索。

1、第三章,Java面向对象编程基础,3.1 java的类3.1.1 创建对象与定义构造函数3.1.2 系统定义的类3.2 方法3.3静态成员3.4包3.5访问控制符,提纲,Java 类,掌握类的定义形式,java类的结构掌握对象实例化方法,教学要求,类是定义一个对象的数据和方法的蓝本;对象代表现实世界中可以明确标识的任何事物,包括状态和行为,用户定义的类,定义格式:修饰符 class 类名 extends 父类名 implements 接口名 类属性声明;类方法声明;,用户定义的类,修饰符:-访问控制符-抽象类(abstract)-最终类(final),class juxing int x;in

2、t y;int S()return x*y;,属性,方法,类定义示例,类定义示例,class PhoneCard long cardNumber;private int password;double balance;String connectNumber;boolean connected;boolean performConnection(long cn,int pw)double getBalance()void performDial().,属性,方法,创建对象,格式:类名 新建对象名 new 构造函数(参数);eg:,Phonecard mycard=new phonecard()

3、,2 使用对象的数据和方法public class testcirclepublic static void main(String args)circle mycircle=new circle();System.out.println(the area of the circle of radius+mycircle.radius+is+mycircle.findarea();class circle double radius=1.0;double findarea()return radius*radius*3.14159;objectname.method引用对象的方法 object

4、name.data引用对象的数据,构造函数,构造函数名与类名同名,缺省形式:A()构造函数没有返回类型构造函数主要是完成对象的初始化工作构造函数在构造类对象时被系统自动调用执行,不可被显式调用执行,构造函数示例,class PhoneCard long cardNumber;private int password;double balance;PhoneCard(long lc,int ip,double db)cardNumber=lc;password=ip;balance=db;,无返回类型,没有return语句,和类名一致,PhoneCard m1=new PhoneCard();,

5、PhoneCard(),PhoneCard m1=new PhoneCard(5,6,82.5);,例:使用构造函数,public class TestCircleWithConstructorspublic static void main(String args)Circle myCircle=new Circle(5.0);System.out.println(The area of the circle of radius+myCircle.radius+is+myCircle.findArea();Circle yourCircle=new Circle();System.out.p

6、rintln(The area of the circle of radius+yourCircle.radius+is+yourCircle.findArea();class Circle double radius;Circle()radius=1.0;Circle(double r)radius=r;double findArea()return radius*radius*3.14159;,构造函数示例,类中变量的默认初始化Java中,一个类中如果没有定义构造方法,则编译器会自动生成一个没有参数的构造方法,用来初始化类对象,并将类中变量自动初始化为该类型的默认值:,整型初始化为0;实型

7、初始化为0.0f、0.0d;逻辑型初始化为false;字符型初始化为u0000;类对象初始化为null,表明不指向任何内存地址的引用如果类中定义了构造方法,则不会自动生成没有参数的构造方法。,class Department int m_DeptNo=10;String m_DeptName;int m_DeptTotalEmp=30;Department(int dno,String dname,int total)m_DeptNo=dno;m_DeptName=dname;m_DeptTotalEmp=total;Department d0=new Department(),成员变量赋值,

8、Department d1=new Department(45,”lg”,20)d1 Department d2=new Department(33,”lg”,20)d2,class Person String name;char sex;int age;double height;Person(String s,char ch,int k,double d)name=s;sex=ch;age=k;height=d;String show()return 姓名:+name+性别:+sex+年龄:+age+身高:+height;,课堂练习,public class deftype public

9、static void main(String args)Person obj1=new Person(张勇,男,22,1.75);System.out.println(obj1.show();obj1.name=李涛;obj1.sex=女;System.out.println(obj1.show();,对象的内存模型如果一个对象没有被初始化,则该对象为null,表示不指向任何内存地址的引用。如果一个对象赋给另一个对象,则两个对象的引用地址相同,它们实际上是同一个对象,只是具有不同的名称。,简单类型赋值 I=j 对象类型赋值 c1=new circle(5);c2=new circle(5);

10、赋值前 赋值后 赋值前 c1=c2 赋值后,i,i,1,2,J,j,2,2,c1,c2,c1,C2,C1:circleRadius=5,C2:circleRadius=9,简单类型变量和对象类型变量的区别:,public class testequal public static void main(String args)Person p1=new Person(张勇,男,22,1.75);Person p2=new Person(张勇,男,22,1.75);System.out.println(p1=p2:+(p1=p2);Person p3=p1;System.out.println(p

11、3=p1:+(p3=p1);p3.name=网友;System.out.println(p1.name:+p1.name);class Person String name;char sex;int age;double height;Person(String s,char ch,int k,double d)name=s;sex=ch;age=k;height=d;,方法,1.定义格式修饰符 返回类型 方法名(形式参数表),2 方法调用格式格式类名或对象名.方法名(实参表),3、说明1)方法名为合法标志符2)形式参数表是用,隔开的变量定义表每个变量必须逐个说明可以为空,或为void放在()

12、内,()是方法的标志,方法,3)方法体4)返回类型与函数返回值有返回值函数:返回类型可以是任何已有类型函数体中必须使用return 表达式;结束函数的运行表达式类型须与返回类型匹配方法体中在return 语句后的语句不会被执行无返回值的函数:返回类型必须说明为void函数体中可用return 语句或不用,方法的重载,方法重载是指在同一个类里面,有两个或两个以上具有相同名称,不同参数序列的方法。例如,三角型类可以定义多个名称为area的计算面积的方法,有的接收底和高做参数,有的接收3条边做参数。这样做的好处是,使开发人员不必为同一操作的不同变体而绞尽脑汁取新的名字,同时也是使类的使用者可以更容易

13、地记住方法的名称。1方法重载的规则2重载方法的匹配,Public void println(boolean x)Public void println(int x)Public void println(char x)Public void println(long x)Public void println(float x)Public void println(double x)Public void println(String x),递归,程序由方法组成,而方法又以层次的方式调用其他的方法,但有些时候,这些方法需要调用自身从而方便地求解一些特殊的问题。递归方法就是自调用方法,在方法体内

14、直接或间接地调用自己,即方法的嵌套是方法本身。递归的方式分为2种:直接递归和间接递归,下面分别介绍这2种递归。,字符串常量(对象)构造方法 public String()pubic String(String s)public String(char value),java.lang.String,String str1=“hello”String s1=new String();string s1=new String(abcd);char a=new chare,f,g,h;String s1=new String(a);,abcd,获取字符串长度public int length()判断

15、字符串的前缀和后缀public boolean startsWith(String prefix)public boolean endsWith(String suffix),java.lang.String,s1.length()s1.startsWith(jk)s1.endWith(jk),4,false,false,字符串中单个字符查找public int indexOf(int ch)public int indexOf(int ch,int fromIndex)public int lastIndexOf(int ch)public int lastIndexOf(int ch,in

16、t fromIndex)public char charAt(int index),java.lang.String,s1.charAt(2)s1.indexOf(e)s1.indexOf(e,1),g,0,-1,s1.indexOf(ef)s1.indexOf(ef“,1)s1.substring(0,2),0,-1,efg,替换字符串中的字符 public String replace(char so,char dst)字符串大小写转换 public String toLowerCase()public String toUpperCase(),java.lang.String,Strin

17、g s2=s1.replace(e,a)s2=s1.toUpperCase(),比较两个字符串public int compareTo(String anotherString)public boolean equals(Object anObject)连接字符字串public String concat(String str)基本数据类型转换为字符串 public static String valueOf(X x),java.lang.String,pareTo(dddddddddddd)s1.equals(“efgh”)s1.equalsIgnore(“EFGH”)String s3=s

18、1.concat(“ijk”);String s4=String.valueOf(56);,1,true,true,public class Stringdemo public static void main(String args)String s=string test;int stringlength=s.length();/获得字符串长度 System.out.println(stringlength=+stringlength);boolean startsTest=s.startsWith(str);/检查字符串前缀与后缀 boolean endsTest=s.endsWith(

19、est);System.out.println(startTest=+startsTest+endTest=+endsTest);int blankindex=s.indexOf(s);/查询特定字符位置 System.out.println(blankindex=+blankindex);int substringindex=s.indexOf(est);/查询特定子串位置 System.out.println(substringindex=+substringindex);int lastindex=s.lastIndexOf(s);/逆向查询特定子串或字符位置 System.out.pr

20、intln(lastindex=+lastindex);,String s2=string testa;String s3=string testb;/字符串比较 int compare=pareTo(s3);System.out.println(compare=+compare);boolean equaltest=s2.equals(s);/字符串相等判断 System.out.println(equaltest=+equaltest);char singlechar=s.charAt(3);/得到特定位置的字符 System.out.println(singlechar=+singlec

21、har);String substring=s.substring(3);/得到从特定位置开始的子串 System.out.println(substring=+substring);String s4=string test;String trimstring=s4.trim();/除去字符串两端的空白字符 System.out.println(trimstring=+trimstring);String uppercase=trimstring.toUpperCase();/得到字符串大写与小写 String lowercase=trimstring.toLowerCase();Syste

22、m.out.println(uppercase=+uppercase);System.out.println(lowercase=+lowercase);,stringlength=12startTest=true endTest=falseblankindex=0substringindex=8lastindex=9compare=-1equaltest=falsesinglechar=isubstring=ing test trimstring=string testuppercase=STRING TESTlowercase=string test,构造方法public StringBu

23、ffer()public StringBuffer(int length)public StringBuffer(String str)字符串变量的扩充、修改与操作public StringBuffer append(String str)public StingBuffer insert(int offset,char ch)public StringBuffer deleteCharAt(int s)public StringBuffer delete(int start,int end)public StringBuffer reverse()public StringBuffer se

24、tCharAt(int in,char ch),java.lang.StringBuffer,StringBuffer s1=new StringBuffer();StringBuffer s2=new StringBuffer(abcd);StringBuffer s3=new StringBuffer(6);s2.insert(1,gggg);s2.setCharAt(1,d);s3.append(odj);,s2.deleteCharAt(1);s2.reverse(),public class stringbufferdemo public static void main(Strin

25、g args)StringBuffer buffer=new StringBuffer();buffer.append(s);buffer.append(tringbuffer);System.out.println(buffer.charAt(1);System.out.println(buffer.capacity();System.out.println(buffer.indexOf(tring);System.out.println(buffer=+buffer.toString();,t161buffer=stringbuffer,数组(对象),1.数组(存储一组同类型的数据)1)声

26、明与创建(分三步走)声明数组 数据类型 数组名 int a;int a;创建数组空间 数组名new 数据类型数组大小 a=new int5;int a=new int5;,2)数组的初始化和处理Length(返回数组的大小)double a=new double10;a.length=10下标0-arrayobject.length-1 数组元素的表示数组名【下标】数组元素赋值for(int i=0;ia.length;i+)ai=i;同时创建 int b=1,2,3,4,5;,对象数组Circle circlearray=new circle10;For(int i=0;icirclearr

27、ay.length;i+)circlearrayi=new circle();,class circle float fradius;int x,y;circle(float fradius,int x,int y)this.fradius=fradius;this.x=x;this.y=y;public float computearea()return(float)Math.PI*fradius*fradius;public class objectarraydemo public static void main(String args)circle arraycircle=new ci

28、rcle(2.0f,0,0),new circle(4.0f,1,1),new circle(5.0f,2,2);int i;float farea=0;for(i=0;iarraycircle.length;i+)farea=putearea();System.out.println(di+(i+1)+cirlce area:+farea);,/数组符值是把同一数组空间取了一个别名public class arrayfuzhi public static void main(String args)int a=1,2,3,4;int d=new int5,6,7,8;a=d;for(int

29、i=0;ia.length;i+)System.out.println(a+i+=+ai+nd+i+=+di);d0=56;System.out.println(a0=+a0+nd0=+d0);,a0=5d0=5a1=6d1=6a2=7d2=7a3=8d3=8a0=56d0=56,/数组拷贝class testarraypublic static void main(String args)int num1=new int1,2,3;int num2=new int3;System.arraycopy(num1,0,num2,0,num1.length);for(int i=0;inum1.l

30、ength;i+)System.out.println(num2i);1 2 3,class testarraypublic static void main(String args)int num1=new int1,2,3;int num2=new int10;System.arraycopy(num1,1,num2,8,2);for(int i=0;inum2.length;i+)System.out.println(num2i);,0000000023,2.二维数组1)定义2)性质实质是一维数组的一维数组length是行数3)操作二维数组元素下标访问元素交换,/二维数组int f=2,

31、3,4,4,5,6;System.out.println(f.length=+f.length);/二维数组是数组的数组int g=new int 2;g0=new int5;g1=new int5/创建多维数组空间时可把行和列分开f=new int 6;for(int i=0;if.length;i+)fi=new int i+1;,public class arraydemopublic static void main(String args)int narray=new int 4;narray0=new int1;narray1=new int2;narray2=new int3;n

32、array3=new int4;int ncounter=0;for(int m=0;mnarray.length;m+)for(int n=0;nnarraym.length;n+)narraymn=ncounter;ncounter+;for(int m=0;mnarray.length;m+)for(int n=0;nnarraym.length;n+)System.out.print(narraymn+);System.out.println();,数组的常用操作,1 数组排序Public static void Sort(X a)Public static void Sort(X a

33、,int fromindex,int toinndex)2 查找制定元素Public static void binarySearch(X a,X key)3 比较数组中的元素Arreys.equals(),4.3类的修饰符,ABSTRACT抽象类:没有具体对象的概念类。特点:抽象出共同特点,是所有子类的公共属性的集合优点:利用公共属性提高开发程序和维护程序的效率(1)abstract 抽象类:没有自己的对象 abstract class A.A a1=new A();,public class concretecircle private double radius;public void

34、setradius(double radius)this.radius=radius;public double getradius()return radius;public void render()System.out.printf(draw an%f concretecirclen,getradius();,public class hollowcircle private double radius;public void setradius(double radius)this.radius=radius;public double getradius()return radius

35、;public void render()System.out.printf(draw an%f hollowcirclen,getradius();,public abstract class abstractcircle protected double radius;public void setradius(int radius)this.radius=radius;public double getradius()return radius;public abstract void render();,public class concretecircle extends abstr

36、actcircle public concretecircle();public concretecircle(double radius)this.radius=radius;public void render()System.out.printf(draw an%f concretecirclen,getradius();,public class hollowcircle extends abstractcircle public hollowcircle();public hollowcircle(double radius)this.radius=radius;public voi

37、d render()System.out.printf(draw an%f hollowecircle,getradius();,public class circledemo public static void main(String args)rendercircle(new concretecircle(3.33);rendercircle(new hollowcircle(10.2);public static void rendercircle(abstractcircle circle)circle.render();,用 abstract修饰的类(称为抽象类)没有自身的对象,此

38、类的对象均为其子类的对象.抽象类就是没有具体对象的概念类 不能创建抽象类的对象抽象类里可以预留一部分方法给子类实现 抽象类可以提高开发和维护的效率,抽象类,抽象类使用示例,(2)final 最终类:没有子类的类 final class B.class C extends B.特点:有固定作用,用来完成某种标准功能的类。注意:ABSTRACT 和FINAL不能同时修饰一个类,类的修饰符,被final修饰的类不可能有子类被final修饰的类通常是有固定作用、用来完成某种标准功能的类,如系统中用于网络通信的Socket类、InetAddress类都是final类安全与性能优化,最终类,4.4 域,变

39、量:保存类或对象的数据类定义中声明,创建类对象时分配空间,保存对象数据例4-3 TestField域变量可以被访问控制符和非访问控制符修饰,局部变量不能被访问控制符和STATIC 修饰,局部变量的作用范围在这个方法内。域变量可以不显式赋初值,局部变量必须显式赋初值域变量随对象创建而创建,局部变量在方法调用式创建,4.4 域,静态域:类的域,保存在类的内存区域的公共存储单元例4-4 例staticexample,statictest静态初始化器:对类自身进行初始化;类加入内存时系统调用执行;不是方法,无方法名返回值和参数列表。Static 例4-5,public class testcar pu

40、blic static void main(String args)car obj1=new car();car obj2=new car();car obj3=new car();System.out.println(car.counter=+car.counter);System.out.println(car.counter=+obj1.counter);System.out.println(car.counter=+obj2.counter);System.out.println(car.counter=+obj3.counter);class car public static in

41、t counter=0;public car()counter+;,public class staticexample static int globalcount=0;public static void main(String args)staticexample example1=new staticexample();staticexample example2=new staticexample();example1.globalcount+;example2.globalcount+;System.out.println(globalcount of example1=+exam

42、ple1.globalcount);System.out.println(globalcount of example2=+example2.globalcount);System.out.println(globalcount=+staticexample.globalcount);,class count private int serial;private static int counter=0;count()counter+;serial=counter;int getserial()return serial;class staticvarstatic int x=100;publ

43、ic class statictest public static void main(String args)count c1=new count();count c2=new count();System.out.println(c1.counter);System.out.println(c2.counter);System.out.println(c1.getserial();System.out.println(c2.getserial();System.out.println(staticvar.x+);System.out.println(staticvar.x+);,final

44、:最终域,修饰常量的修饰符 final int j=9;如果一个属性被声明为final 性质,则它的取值在整个程序运行过程中都不会改变注意:最终属性用来定义常量的类型最终属性必须初始化最终属性在整个程序运行过程中不可改变因为类的对象的常量成员其值都一致,为了节省空间,通常声明为static例如:static final m_Minsalary=400;Public static final int max_value,最终属性,class A int i=1;public void g()public class test final int j=9;public static final i

45、nt k=20;final A a1=new A();final A a2;,没有初始化,最终属性使用示例,初始化顺序,12,Class tag tag(int maker)system.out.println(“tag(“+maker+”)”);Class card tag t1=new tag(1);Card()system.out.println(“card()”);t3=new tag(33);tag t2=new tag(2);Void f()system.out.println(“f()”);tag t3=new tag(3);Public class orderofinitial

46、izationPublic static void main(string args)card t=new card();t.f();CLASS中的初始化次序取决与变量在CLASS 中的定义次序 变量会在任何函数被调用前完成初始化,class objectcreationtestclass testclass1=new testclass(fieldvalue);static testclass testclass2=new testclass(staticvalue);statictestclass2.makeinner();public objectcreation()System.out

47、.println(objectcreation init);public objectcreation(String name)System.out.println(objectcreation+name+init);public static void main(String args)objectcreation objectcreation1=new objectcreation(object1);objectcreation objectcreation2=new objectcreation(object2);class testclass public testclass(Stri

48、ng name)System.out.println(testclass+name+init);void makeinner()System.out.println(makeinner()invoked);,testclass staticvalue initmakeinner()invokedtestclass fieldvalue initobjectcreation object1 inittestclass fieldvalue initobjectcreation object2 init,Class bowlbowl(int marker)system.out.println(“b

49、owl(“+maker+”)”);Void f(int marker)system.out.println(“f(“+maker+”)”);Class tableStatic bowl b1=new bowl(1);Table()system.out.println(“table()”);b2.f(1);Void f2(int marker)system.out.println(“f(“+maker+”)”);Static bowl b2=new bowl(2);,Class cupboardbowl b3=new bowl(3);Static bowl b4=new bowl(4);cupb

50、oard()system.out.println(“cupboard()”);b4.f(2);Void f3(int marker)system.out.println(“f3(“+maker+”)”);Static bowl b5=new bowl(5);,Public class staticinitializationPublic static void main(string args)system.out.println(“creating new cupboard()in main”);New cupboard();system.out.println(“creating new

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 建筑/施工/环境 > 农业报告


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号