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

上传人:本田雅阁 文档编号:2565909 上传时间:2019-04-09 格式:PPT 页数:98 大小:637.51KB
返回 下载 相关 举报
第四Java面向对象编程基础.ppt_第1页
第1页 / 共98页
第四Java面向对象编程基础.ppt_第2页
第2页 / 共98页
第四Java面向对象编程基础.ppt_第3页
第3页 / 共98页
亲,该文档总共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 j

2、uxing int x; int 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:

3、,Phonecard mycard=new phonecard(),2 使用对象的数据和方法 public class testcircle public 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*

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

5、=db; ,无返回类型,没有return语句,和类名一致,PhoneCard m1=new PhoneCard();,PhoneCard (),PhoneCard m1=new PhoneCard(5,6,82.5);,例:使用构造函数,public class TestCircleWithConstructors public static void main(String args) Circle myCircle = new Circle(5.0); System.out.println(“The area of the circle of radius “ + myCircle.rad

6、ius + “ is “ + myCircle.findArea(); Circle yourCircle = new Circle(); System.out.println(“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.

7、14159; ,构造函数示例,类中变量的默认初始化 Java中,一个类中如果没有定义构造方法,则编译器会自动生成一个没有参数的构造方法,用来初始化类对象,并将类中变量自动初始化为该类型的默认值:, 整型初始化为0; 实型初始化为0.0f、0.0d; 逻辑型初始化为false; 字符型初始化为u0000; 类对象初始化为null,表明不指向任何内存地址的引用 如果类中定义了构造方法,则不会自动生成没有参数的构造方法。,class Department int m_DeptNo=10; String m_DeptName; int m_DeptTotalEmp=30; Department(int

8、 dno,String dname,int total) m_DeptNo=dno; m_DeptName=dname; m_DeptTotalEmp=total; Department d0 =new Department(),成员变量赋值,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

9、k,double d) name=s; sex=ch; age=k; height=d; String show() return “姓名:“+name+“性别:“+sex+“年龄:“+age+“身高:“+height; ,课堂练习,public class deftype public 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.s

10、how(); ,对象的内存模型 如果一个对象没有被初始化,则该对象为null,表示不指向任何内存地址的引用。如果一个对象赋给另一个对象,则两个对象的引用地址相同,它们实际上是同一个对象,只是具有不同的名称。,简单类型赋值 I=j 对象类型赋值 c1=new circle(5); c2=new circle(5); 赋值前 赋值后 赋值前 c1=c2 赋值后,i,i,1,2,J,j,2,2,c1,c2,c1,C2,C1:circle Radius=5,C2:circle Radius=9,简单类型变量和对象类型变量的区别:,public class testequal public static

11、 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(“p3=p1:“+(p3=p1); p3.name=“网友“; System.out.println(“p1.name:“+p1.name); class Person String name; char sex; int age; double height

12、; Person(String s,char ch,int k,double d) name=s; sex=ch; age=k; height=d; ,方法,1.定义格式 修饰符 返回类型 方法名 (形式参数表) ,2 方法调用格式 格式 类名或对象名. 方法名(实参表),3、说明 1)方法名为合法标志符 2)形式参数表 是用,隔开的变量定义表 每个变量必须逐个说明 可以为空,或为void 放在( )内,( )是方法的标志,方法,3)方法体 4)返回类型与函数返回值 有返回值函数: 返回类型可以是任何已有类型 函数体中必须使用return 表达式;结束函数的运行 表达式类型须与返回类型匹配 方

13、法体中在return 语句后的语句不会被执行 无返回值的函数: 返回类型必须说明为void 函数体中可用return 语句或不用,方法的重载,方法重载是指在同一个类里面,有两个或两个以上具有相同名称,不同参数序列的方法。例如,三角型类可以定义多个名称为area的计算面积的方法,有的接收底和高做参数,有的接收3条边做参数。这样做的好处是,使开发人员不必为同一操作的不同变体而绞尽脑汁取新的名字,同时也是使类的使用者可以更容易地记住方法的名称。 1方法重载的规则 2重载方法的匹配,Public void println( boolean x) Public void println( int x)

14、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),递归,程序由方法组成,而方法又以层次的方式调用其他的方法,但有些时候,这些方法需要调用自身从而方便地求解一些特殊的问题。递归方法就是自调用方法,在方法体内直接或间接地调用自己,即方法的嵌套是方法本身。递归的方式分为2种:直接递归和间接递归,下面分别介绍这2种递归。,字符串常量(对象) 构造方法 pub

15、lic 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() 判断字符串的前缀和后缀 public boolean startsWith(String prefix) public bool

16、ean 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,int fromIndex) public char charAt(int index),java

17、.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,String s2=s1. replace(e,a) s2=s1. toUppe

18、rCase(),比较两个字符串 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=s1. concat(“ijk”); S

19、tring 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(“est“)

20、; 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);/逆向查询特定子串或字符位置 Sy

21、stem.out.println(“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.pr

22、intln(“singlechar=“+singlechar); 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 lo

23、wercase=trimstring.toLowerCase(); System.out.println(“uppercase=“+uppercase); System.out.println(“lowercase=“+lowercase);,stringlength=12 startTest=true endTest=false blankindex=0 substringindex=8 lastindex=9 compare=-1 equaltest=false singlechar=i substring=ing test trimstring=string test uppercase

24、=STRING TEST lowercase=string test,构造方法 public StringBuffer() 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 sta

25、rt,int end) public StringBuffer reverse() public StringBuffer setCharAt(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.deleteC

26、harAt(1); s2.reverse(),public class stringbufferdemo public static void main(String 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“); Sys

27、tem.out.println(“buffer=“+buffer.toString(); ,t 16 1 buffer=stringbuffer,数组(对象),1. 数组(存储一组同类型的数据) 1)声明与创建(分三步走) 声明数组 数据类型 数组名 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 数组元素的表示数组名【下标】 数组元素赋值f

28、or (int i=0;ia.length;i+) ai=i; 同时创建 int b=1,2,3,4,5;,对象数组 Circle circlearray=new circle10; For (int i=0;icirclearray.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(

29、float)Math.PI*fradius*fradius; public class objectarraydemo public static void main(String args) circle arraycircle=new circle(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:“+fa

30、rea); ,/数组符值是把同一数组空间取了一个别名 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 i=0;ia.length ;i+) System.out .println (“a“+i+“=“+ai+“nd“+i+“=“+di); d0=56; System.out .println (“a0=“+a0+“nd0=“+d0);,a0=5 d0=5 a1=6 d1=6 a2=7 d2=7 a3=8 d3=8 a0=

31、56 d0=56,/数组拷贝 class testarray public 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.length;i+) System.out.println(num2i); 1 2 3,class testarray public static void main(String args) int num1=new int1,2,3; int num

32、2=new int10; System.arraycopy(num1,1,num2,8,2); for(int i=0;inum2.length;i+) System.out.println(num2i); ,0 0 0 0 0 0 0 0 2 3,2.二维数组 1)定义 2)性质 实质是一维数组的一维数组 length是行数 3)操作 二维数组元素下标访问 元素交换,/二维数组 int f=2,3,4,4,5,6; System.out .println (“f.length=“+f.length ); /二维数组是数组的数组 int g=new int 2; g0=new int5;g1=

33、new int5 /创建多维数组空间时可把行和列分开 f=new int 6; for(int i=0;if.length ;i+) fi=new int i+1;,public class arraydemo public static void main(String args) int narray=new int 4 ; narray0=new int1; narray1=new int2; narray2=new int3; narray3=new int4; int ncounter=0; for(int m=0;mnarray.length;m+) for(int n=0;nna

34、rraym.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,int fromindex ,int toinndex) 2 查找制定元素 Public static void binary

35、Search(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 setradius(double radius) this.radius=radius; public d

36、ouble 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; public void render() System.out.printf

37、(“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 abstractcircle public concretecirc

38、le(); 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 void render() System.

39、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修饰的类(称为抽象类)没有自身的对象,此类的对象均为其

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

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

42、ar public 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

43、 car public static int 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(

44、“globalcount of example1=“ +example1.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()retur

45、n serial; class staticvarstatic int x =100; public 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.printl

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

47、blic void g() public class test final int j=9; public static final int k=20; final A a1=new A(); final A a2;,没有初始化,最终属性使用示例,初始化顺序,1 2,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 orderofinitialization Public static void main(string args) card t=new card

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

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1