进一步讨论对象和类.ppt

上传人:牧羊曲112 文档编号:4997391 上传时间:2023-05-28 格式:PPT 页数:30 大小:232.63KB
返回 下载 相关 举报
进一步讨论对象和类.ppt_第1页
第1页 / 共30页
进一步讨论对象和类.ppt_第2页
第2页 / 共30页
进一步讨论对象和类.ppt_第3页
第3页 / 共30页
进一步讨论对象和类.ppt_第4页
第4页 / 共30页
进一步讨论对象和类.ppt_第5页
第5页 / 共30页
点击查看更多>>
资源描述

《进一步讨论对象和类.ppt》由会员分享,可在线阅读,更多相关《进一步讨论对象和类.ppt(30页珍藏版)》请在三一办公上搜索。

1、第 五 章,2023/5/28,1,进一步讨论对象和类,2,第五章 进一步讨论对象和类,5.1 Java中方法及类的访问规则5.2 Java中的按值传送5.3 Java中的抽象类、接口和程序包5.4 对象的构造函数5.5 内部类,3,5.1 Java中方法及类的访问规则,public class AccessInClass1 int sex;void m1()sex=1;int m2()sex=2;return sex;void m3()m1();m2();,前提:若某个方法可直接访问其它成员,该方法不能是静态的,即方法前面不能有static关键字。,4,5.1 Java中方法及类的访问规则,

2、public class AccessInClass2 int sex;static int salary;void m1()sex=1;salary=1;int m2()sex=2;salary=2;return sex;void m3()m1();m2();m4();m5();static void m4()static void m5()AccessInClass2 aic=new AccessInClass2();aic.m1();aic.m2();aic.m3();aic.m4();m4();/m1();/error/m2();/error/m3();/error aic.sex=3

3、;aic.salary=3;alary=3;/sex=3;error,注意:非静态方法访问静态或非静态成员方式一样,可直接访问。静态方法访问非静态成员必须创建对象。静态方法可不创建对象,直接访问静态成员和静态方法,也可创建对象来访问。,5,5.1 Java中方法及类的访问规则,public class AccessOtherClass1 void m1()OtherClass1 oc1=new OtherClass1();oc1.sex=1;System.out.println(“sex=”+oc1.sex);public static void main(String args)OtherC

4、lass1 oc2=new OtherClass1();oc2.sex=0;System.out.println(“sex=”+oc2.sex);oc2.m2();class OtherClass1 int sex;void m2()System.out.println(“m2()”);,注意:文件中包含两个或以上类,那只能有一个类前加public关键字,且文件名必须与这个类名一致。AccessOtherClass1访问OtherClass1,无论何情况下,都需创建该类对象才可以访问它的成员(域或方法)。若两个类名前都没有public,可以以任何名字把文件存盘。,6,5.2 Java中的按值传

5、送,当“按值”传送自变量时,方法调用不会改变自变量的值。当对象实例作为自变量传送给方法时,自变量的值是对象的引用可修改该引用指向的对象内容。(P93),7,public void changeStr(String value)System.out.println(before value is:+value);value=new String(different);System.out.println(after value is:+value);public void changeObjValue(PassTest ref)System.out.println(before ref.ptVa

6、lue is:+ref.ptValue);ref=new PassTest();ref.ptValue=99f;System.out.println(after ref.ptValue is:+ref.ptValue);,8,5.3 Java中的抽象类、接口和程序包,抽象类是指不能直接被实例化的类。一般作为其它类的超类,与final类正好相反。抽象类中的抽象方法在该类中定义但不在该类中提供实现,由继承类提供细节。public abstract class SomeAbstractClass void method1()System.out.println(“Hi,Java”);abstract

7、 void method2();,public abstract class SomeAbstractClass1 abstract void method1();abstract void method2();,9,5.3 Java中的抽象类、接口和程序包,抽象类的定义格式为:public abstract class 类名.抽象类方法的定义格式为:public abstract(参数表);抽象类不能创建实例,由非抽象类的子类创建定义抽象方法的类必须是抽象类。对于抽象方法不允许使用final修饰abstract方法,10,5.3 Java中的抽象类、接口和程序包,/两个类Circle和Rec

8、tangle,完成相关参数的计算 class Circle public int r;Circle(int r)this.r=r/this指这个对象的 public int area()return 3*r*r;/取近似 class Rectangle public int width,height;/这里不需this Rectangle(int w,int h)width=w,height=h;public int area()return width*height;,11,5.3 Java中的抽象类、接口和程序包,假设有若干个Circle,以及若干个Rectangle,希望计算它们的总面积

9、,直截了当的做法是将它们分别放到两个数组中,用两个循环,加上一个加法,这种做法是不漂亮的。如果还有其它形状,如triangle,ellipses等,上述方法显得“累赘”,我们希望有一种统一的表示,例如用一个数组shape,接受所有的形状,然后用 for(i=0;ishape.length;i+)area_total+=shapei.area();,12,abstract class Shape abstract float area();class Circle extends Shape public int r;Circle(int r)this.r=r;public float area

10、()return 3.14*r*r;class Square extends Shape public int width,height;Rectangle(int w,int h)width=w,height=h;public float area()return width*height;,13,5.3 Java中的抽象类、接口和程序包,利用接口可实现多重 继承(子类可同时实现多个接口)。接口的作用和抽象类 类似,指定原型,不直 接定义方法的内容。关键字implement用 来实现方法,即在使用时要用给出方法的实际内容。接口中的方法和变量 是public的。,interface Stack

11、 void push(object x);object pop();,class A extends Applet implements Stack void push(object x);/具体内容 object pop();/具体内容,14,5.3 Java中的抽象类、接口和程序包,接口的声明格式为:接口修饰符 interface 接口名称 extends父类名.接口中定义的成员变量都必须赋初值,系统默认为终极类变量,即自动增加final与static关键字。接口与类的继承不同之处在于实现接口的类不从该接口的定义中继承任何行为,在实现过程中,这个类还可同时实现其它接口。完成接口的类须实现接

12、口中的所有抽象方法。,15,5.3 Java中的抽象类、接口和程序包,程序包:相当于其它语言中的库函数。打包,package graphics;class Square;class Circle;class Triangle;,使用程序包中的类要用import命 令。表示路径,*表示使用包中的 所有类。;import java.io.*;,16,5.4 对象的构造函数,class Demo1/数组的配置 public static void main(String args)int array;array=new int55;array14=5;,class MyClass/对象实体的产生 i

13、nt data=5;class Demo2 public static void main(String args)MyClass obj;/建立一个引用 obj=new MyClass();System.out.println(obj.data);,17,5.4 对象的构造函数,constructor(构造函数),在一个类中和类同名的方法叫构造函数。系统在产生对象时会自动执行。,class Point int x,y;Point()x=0;y=0;Point(int new_x,int new_y)x=new_x;y=new_y;,class UsePoint Point point_A=n

14、ew Point();Point point_B=new Point(5,7);,18,5.4 对象的构造函数,构造函数应包含哪些内容?构造函数多半定义一些初值或内存配置工作,一个类可以有多个构造函数(重载),根据参数的不同决定执行哪一个。如果程序中没有定义构造函数,则创造实例时使用的是缺省函数,它是一个无内容的空函数。若类为public,但是构造方法为private,那么在其它类中也不能创建该类的对象。即若不想让别人访问一个类,可用private修饰它的构造方法,19,5.4 对象的构造函数,thisthis是Java中的一个关键字,指自己这个对象。this的作用是要将自己这个对象当作参数,

15、传送给别的对象中的方法。,class ThisClass public static void main()Bank bank=new Bank();bank.someMethod(this);,class Circle int r;Circle(int r)this.r=r;public area()return r*r*3;,20,5.4 对象的构造函数,supersuper指这个对象的父类super用来引用父类中的方法及变量数据.public class Apple extends Fruits public Apple(int price)super(price);以上句子表示使用超类

16、的构造函数生成实例super必须是子类构造函数的第一条语句,21,5.4 对象的构造函数,使用super调用父类的构造方法class Student int number;String name;Student()Student(int number,String name)this.number=number;this.name=name;System.out.println(“I am”+name+“my number is”+number);class UStudent extends Student boolean 婚否;UStudent(int number,String name,

17、boolean b)super(number,name);婚否=b;(“婚否=”+婚否);,public class Example public static void main(String args)UStudent zhang=new UStudent(9801,“张红”,false);,I am 张红 my number is 9801婚否=false,22,5.4 对象的构造函数,使用super操作被隐藏的成员变量和方法class Sum int n;float f()float sum=0;for(int i=1;i=n;i+)sum=sum+i;return sum;class

18、 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;,23,5.4 对象的构造函数,超类与子类可以有一模一样的静态方法。但静态方法不能被覆盖(重置),只能被隐藏,且子类与父类中的方法须同时为static或同时不是static。,class A static void m()System.out.println(“static method in superClass”);public class Verridden

19、Static1 extends A static void m()System.out.println(“static method in subClass”);public static void main(String args)A obj=new VerriddenStatic1();obj.m();,结果:static method in superClass,24,5.4 对象的构造函数,静态块是在类被调到内存后就开始执行的,一般用于初始化类中的静态成员。它和创建对象或者main方法没有关系。,public class StaticBlock1 static System.out.p

20、rintln(“static block”);public static void main(String args),运行后结果:Static block,25,5.4 对象的构造函数,在创建对象时,对象所在类的所有数据成员会首先进行初始化。,class Children Children(int marker)System.out.println(“Children(”+marker+“)”);class Parent Children child1=new Children(1);Parent()System.out.println(“Parent()”);Children child3

21、=new Children(33);Children child2=new Children(2);void relation()System.out.println(“relation()”);Children child3=new Children(3);public class ObjectInit public static void main(String args)System.out.println(“main begins”);Parent mother=new Parent();mother.relation();,main beginChildren(1)Children(

22、2)Children(3)Parent()Children(33)relation(),26,5.4 对象的构造函数,在创建对象时,对象所在类的所有数据成员会首先进行初始化。,class Children Children(int marker)System.out.println(“Children(”+marker+“)”);class Parent Children child1=new Children(1);Parent()System.out.println(“Parent()”);Children child3=new Children(33);Children child2=n

23、ew Children(2);void relation()System.out.println(“relation()”);static Children child3=new Children(3);public class ObjectInit public static void main(String args)System.out.println(“main begins”);Parent mother=new Parent();mother.relation();,main beginChildren(3)Children(1)Children(2)Parent()Childre

24、n(33)relation(),27,5.4 对象的构造函数,创建对象时,对象所在类的所有数据成员首先进行初始化。构造方法的作用就是初始化静态对象在非静态对象前初始化,但静态对象只初始化一次。,1、当类调入到内存时,类中的静态成员开始初始化。2、超类的构造方法按照从最高到最低的顺序调用。3、类中的非静态对象初始化。4、调用类的构造方法。,初始化的顺序包括构造方法调用的顺序如下:,28,5.5 内部类,public class Outer1 private int size;public class Inner public int m1(int newSize)size=newSize;ret

25、urn size;/*public static int m2()size=2;return size;*/public void accessInner(int newSize)Inner inner=new Inner();System.out.println(“Inner.m1()=”+inner.m1(5);public static void main(String args)Outer1 outer=new Outer1();outer.accessInner(1);Outer1.Inner inner=outer.new Inner();inner.m1(2);,非静态内部类,1

26、、在内部类中的方法可以直接访问外部类的成员,不需要创建外部类的对象。2、外部类的非静态方法,若要访问内部成员,须创建内部类的对象。3、外部类的静态方法若要访问内部类成员,须先创建外部类对象,再创建内部类对象,最后才可访问。,29,5.5 内部类,class China final String nationalAnthem=义勇军进行曲;Beijing beijing;China()beijing=new Beijing();String getSong()return nationalAnthem;class Beijing String name=北京;void speak()System

27、.out.println(我们是+name+我们的国歌是:+getSong();public class Example public static void main(String args)China china=new China();china.beijing.speak();/China.Beijing beij=china.new Beijing();/beij.speak();,30,5.5 内部类,class Cubic double getCubic(int n)return 0;abstract class Sqrt public abstract double getSq

28、rt(int x);class A void f(Cubic cubic)double result=cubic.getCubic(3);System.out.println(result);public class Example public static void main(String args)A a=new A();a.f(new Cubic()double getCubic(int n)return n*n*n;);Sqrt ss=new Sqrt()public double getSqrt(int x)return Math.sqrt(x);double m=ss.getSqrt(5);System.out.println(m);,匿名类,

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号