对象和类解析课件.ppt

上传人:小飞机 文档编号:2155960 上传时间:2023-01-20 格式:PPT 页数:83 大小:1.56MB
返回 下载 相关 举报
对象和类解析课件.ppt_第1页
第1页 / 共83页
对象和类解析课件.ppt_第2页
第2页 / 共83页
对象和类解析课件.ppt_第3页
第3页 / 共83页
对象和类解析课件.ppt_第4页
第4页 / 共83页
对象和类解析课件.ppt_第5页
第5页 / 共83页
点击查看更多>>
资源描述

《对象和类解析课件.ppt》由会员分享,可在线阅读,更多相关《对象和类解析课件.ppt(83页珍藏版)》请在三一办公上搜索。

1、Objectives(学习目标),To understand objects and classes(理解对象和类)To learn how to declare a class and how to create an object of a class.(学习如何定义类和创建类的对象)To understand the roles of constructors and use constructors to initialize objects.(理解构造方法扮演的角色,会使用构造方法初始化对象)To distinguish between object reference variab

2、les and primitive data type variables(区分引用变量和基本数据类型变量)To declare private data fields with appropriate get and set methods to make class easy to maintain(声明私有成员变量,并为变量提供get和set方法),1,2,Objectives(学习目标),To use the keyword this as the reference to the current object that invokes the instance method(会使用关

3、键字this作为当前对象的引用)To understand the difference between instance and static variables and methods.(理解静态变量和实例变量,静态方法和实例方法的区别)To develop methods with object arguments(能创建以对象作为参数的方法)To store and process objects in arrays(会处理对象数组)To understand the access level to class and members.(理解Java类、成员的访问级别),3,What

4、is Object(什么是对象),An object represents an entity in the real world that can be distinctly identified.(对象代表现实世界中可以明确标识的一个实体)For example,a student,a desk,a circle,a button can all be viewed as objects.All the objects share two characteristics(所有对象都有两方面的特性):State(状态)Behavior(行为),4,对象的状态-变量,每个对象的每个属性都拥有特

5、定值,例如:王小红和朱小雨的体重不一样,体重:60kg,姓名:王小红,年龄:35岁,5,对象的行为方法,方法对象执行的操作,描述对象的行为,打单,收银,方法,刷卡,What is Object?,A software object maintains its state in one or more variables(member variable/data fields/properties).(软件上的对象使用变量来存储状态)A software object implements its behavior with methods.(软件上的对象使用方法来实现它的行为),6,7,类,类

6、概括了同类对象共有的性质:属性和方法,顾客类轿车类收银员类,类是模板,比如说:“人”对象是类的一个实例,比如:“小布什”,小布什,普京,克林顿,8,创建类,Member variable成员变量,构造方法,方法,9,Constructors(构造方法),class Circle double radius=1;Circle()Circle(double newRadius)radius=newRadius;/,Constructors are a special kind of methods that are invoked to initialize objects.(构造方法是一类特殊的

7、方法,用来初始化新对象),Constructors(构造方法),Constructors must have the same name as the class itself.(构造方法的名称必须和类名相同)Constructors do not have a return typenot even void.(构造方法没有返回类型,连void也没有)Constructors are invoked using the new operator when an object is created.(构造方法在创建对象时用new关键字调用)Constructors play the role

8、of initializing objects.(构造方法的作用是初始化新对象)A constructor with no parameters is referred to as a no-arg constructor.(没有参数的构造方法称为无参构造方法),10,11,问题,每个类都必须显式地声明构造方法吗?如果一个类中没有声明构造方法,这个类是不是就没有构造方法?,12,Constructors(构造方法),A class may be declared without constructors.(类可以不声明构造方法)In this case,a no-arg constructor

9、 with an empty body is implicitly declared in the class.(在这种情况下,类中隐含地声明了一个方法体为空的无参构造方法),class Welcome public static void main(String args)System.out.println(“welcome to Java!”);,class Welcome public static void main(String args)System.out.println(“welcome to Java!”);public Welcome(),相当于,Constructors

10、(构造方法),This constructor,called a default constructor,is provided automatically only if no constructors are explicitly declared in the class.(这个构造方法称为默认构造方法,只有当类中没有明确声明构造方法时,默认构造方法才会自动生成),13,14,创建对象,public class Circle double radius=1.0;Circle()Circle(double newRadius)radius=newRadius;double getArea(

11、)return radius*radius*3.14;,public class Test public static void main(String args)Circle c1=new Circle();Circle c2=new Circle(5.0);Java使用new关键字调用构造方法来创建对象和初始化新对象,对象创建过程如下:(1)new关键字创建对象,为对象分配空间,为成员变量赋初值(2)调用构造方法为新对象初始化(3)构造方法会返回新对象的引用,该引用可以赋值给同类型的变量,Declaring/Creating Objectsin a Single Step,15,0 x34

12、AC0,yourCircle:,radius:,5.0,Circle对象,内存地址0 x34AC0,对象的实例成员变量(非static变量)存储在对象所在内存区域,16,Declaring/Creating Objects,类名 变量名;,If reference type does not reference any object,it holds a special literal value,null.如果一个引用变量没有指向任何一个对象,这个引用变量的值是null,Declaring/Creating Objects,18,Overloading constructors构造方法重载,

13、19,Accessing Objects(访问对象),Referencing the objects member variable(访问对象的成员变量):对象的引用.成员变量名Invoking the objects method(调用对象的方法):对象的引用.方法名(实参),20,Accessing Objects,21,创建圆柱类Column,要求如下:包含两个成员变量:h,r,分别代表圆柱的高和底面半径,均为double类型一个构造方法:两个参数,用于为成员变量h和r赋指定的初值一个方法:double getVolume(),用于计算圆柱的体积创建测试类TestColumn,包含mai

14、n方法,main方法中实现下面功能:创建Column对象c1,底面半径为4,高为5创建Column对象c2,底面半径为10,高为10计算c2的体积,存储在变量v中,输出v的值,22,创建类Point,要求如下:包含两个成员变量:x,y,分别代表点的横坐标和纵坐标,均为int类型一个构造方法:两个参数,用于为成员变量x和y赋指定的初值一个方法:void move(int x,int y),用于将点移动到参数指定的坐标创建测试类TestPoint,包含main方法,main方法中实现下面功能:创建Point对象p1,其x坐标为2,y坐标为3创建Point对象p2,其x坐标为5,y坐标为-1将p1移

15、动到(0,0),输出其x坐标和y坐标,23,创建汽车类Car,要求如下:包含三个成员变量:name,price,speed,分别代表汽车的品牌、价格、速度构造方法1:两个参数,用于为成员变量name和price赋指定的初值构造方法2:三个参数,用于为三个成员变量赋指定的初值方法1:void speedUp(int s),用于将车速加到指定值方法2:void stop(),停车,将车速减至0创建测试类TestCar,包含main方法,main方法中实现下面功能:创建Car对象car1,三项属性值自定让car1加速至100,输出car1的车速让car1停车,输出car1的车速,24,public

16、class Welcome public static void main(String args)System.out.print(Welcome to Java!);,创建Welcome对象w,25,public class Rectangle int width;/矩形的宽int height;/矩形的高int x;/矩形左上角点横坐标 int y;/矩形左上角点纵坐标Rectangle(int w,int h,int x1,int y1)width=w;height=h;x=x1;y=y1;void move(int x1,int y1)x=x1;y=y1;int getArea()r

17、eturn width*height;,26,public class Rectangle int width;/矩形的宽int height;/矩形的高Point origin;/原点(左上角点)Rectangle(int w,int h,Point p)width=w;height=h;/补充void move(int x1,int y1)/补充int getArea()return width*height;,1、补充上面代码。2、创建Rectangle对象r,原点为(5,6),宽20,高10计算r的面积,输出3、将r移动至(-1,-1)位置,输出其原点横坐标和纵坐标,27,网购的订单需

18、求:1、订单号 2、成交时间 3、总价 4、订单状态:1-未付款,2-已付款,3-已发货,4-完成 5、收货地址 1)姓名 2)省 3)市 4)街道 5)邮编 6)联系电话,28,创建收货地址类ShippingAddress,要求如下:包含6个成员变量:name,province,city,street,postcode,tel分别代表收件人姓名、省、市、街道、邮编、联系电话一个构造方法:6个参数,用于为6个成员变量赋指定的初值一个方法:setTel(String newTel),用于更改联系电话创建订单类Order包含5个成员变量:orderNo,time,totalPrice addres

19、s,status,分别代表订单号、成交时间、总价、收货地址、订单状态。address变量为ShippingAddress类型,status的初值为1。一个构造方法:4个参数,用于为4个成员变量(除了status)赋指定的初值方法1:void setStatus(int s),用于更改订单状态方法2:void setAddress(ShippingAddress a),用于更改收货地址方法3:void setTel(String newTel),用于更改联系电话,29,结合网购经历思考下面问题:1、用户进行哪一项操作时,程序应该创建一个Order对象?2、用户的哪些操作会调用setStatus(

20、)方法修改订单状态?3、用户的哪一项操作会调用setAddress()方法?,30,Roles of constructor构造方法的作用,class Cat String name=“小白”;String color=“white”;char sex=f;String type=“¥”;Cat c1=new Cat();Cat c2=new Cat();,31,Roles of construct构造方法的作用,class Cat String name;String color;char sex;String type;Cat(String n,String c,char s,String

21、 t)name=n;color=c;sex=s;type=t;Cat c1=new Cat(“花花”,”brown”,f,”$%$”);Cat c2=new Cat(“小黑”,”black”,f,”#”);,32,33,Default Value for member variable成员变量的默认值,The default value of a member variable is(成员变量会自动赋初值):null for a reference type(引用类型的成员变量的初始值为null)0 for a numeric type(数字型的成员变量初始值为0)false for a bo

22、olean type(boolean类型的成员变量初始值为false)u0000 for a char type(char类型的成员变量初始值为u0000),34,Example,public class Test public static void main(String args)int x;/x has no default value String y;/y has no default value System.out.println(“x is”+x);/variables not initialized System.out.println(y is+y);/variables

23、 not initialized,Java assigns no default value to a local variable inside a method(局部变量是没有默认值的).,35,对象的内存模型,Java语言将数据类型分成两类:基础数据类型(Primitive data type):byte,short,int,long,float,double,boolean,char引用类型(Reference type):类、数组、接口A variable of a primitive type holds a value of the primitive type(基础数据类型变量

24、存放的是数据值)int i=10;int j=12;,36,对象的内存模型,a variable of reference type holds a reference to where an object or array is.(引用变量包含对象或者数组的引用地址)Circle c=new Circle(5.0);,0 x34AC0,c:,radius:,5.0,Circle对象,内存地址0 x34AC0,对象的实例成员变量(非static变量)存储在对象的空间,37,Copying Variables,int i=2;int j=i;,j,2,38,Copying Variables,C

25、ircle c1=new Circle(5);Circle c2=new Circle(6);c1=c2;,c1:,0 x34AB6,c2:,radius:,Circle对象,内存地址0 x34AC0,5.0,radius:,Circle对象,内存地址0 x34AB6,6.0,如果一个对象没有任何引用指向它,该对象就成为无用对象(半径为5的圆),可以被垃圾回收,Java虚拟机会自动回收无用对象,释放空间,0 x34AC0,0 x34AB6,无用对象,垃圾,39,class A int x,y;A(int a,int b)x=a;y=b;public class sample public st

26、atic void main(String args)A p2=new A(12,15);A p1=p2;p2.x+;System.out.println(p1.x=+p1.x);,这段程序的输出是什么?,40,Garbage Collection垃圾回收,An object is eligible for garbage collection when there are no more references to that object.(如果一个对象没有任何引用指向它,该对象就成为无用对象,可以被垃圾回收)The JVM will automatically collect the sp

27、ace if the object is not referenced by any variable.(Java虚拟机会自动回收无用对象,释放空间)TIP:If you know that an object is no longer needed,you can explicitly assign null to a reference variable for the object.(如果一个对象不再使用,可以为其赋值为null)例:Circle c=new Circle(25);c=null;,41,垃圾回收,下面的代码执行完后,一共创建了几个对象?多少个对象成为无用对象?1、Circ

28、le c1=new Circle(5.5);2、Circle c2=new Circle();3、new Circle(2.5);4、c1=c2;5、c1=null;,42,class Circle double radius;Circle(double newRadius)radius=newRadius;class TestCircle public static void main(String args)Circle c1=new Circle();c1.radius=-5;/这样的赋值有意义吗?Circle c2=new Circle(-3);/创建这样的圆有意义吗?,43,clas

29、s Circle private double radius;public Circle(double newRadius)if(newRadius=0)radius=newRadius;public double getRadius()return radius;public void setRadius(double newRadius)if(newRadius=0)radius=newRadius;,public class Test()public static void main(String args)Circle c1=new Circle(5);c1.setRadius(7);

30、double r=c1.getRadius();,将成员变量设置为private,防止在类的外部直接使用赋值语句为成员变量赋值,为成员变量提供get和set方法,存取变量值,在set方法内部可以用条件语句保证赋值的有效性,44,为Point、Column、Rectangle、Car类编写get和set方法,45,创建账户类Account,内容如下:成员变量:账户id,账户余额balance,开户日期dateCreated构造方法,为id和dateCreated赋初值为id、balance、dateCreated创建get和set方法取款方法withdraw,从账户提取指定数额存款方法depos

31、it,向账户存指定数额测试类创建一个ID为1122,开户日期为当前日期的账户使用deposit方法存入3000元使用withdraw方法取出2500元输出账户余额,思考,如果某个成员变量在类外不允许修改,编写类的时候要采取哪些措施?如果某个成员变量的值在类外不允许访问(读、写),编写类的时候要采取哪些措施?,46,47,public class TestPassObject public static void main(String args)Circle myCircle=new Circle(1);int n=5;printAreas(myCircle,n);System.out.pri

32、ntln(myCircle.getRadius();System.out.println(n is+n);static void printAreas(Circle c,int times)while(times=1)c.setRadius(c.getRadius()+1);times-;,为方法传递对象参数,48,The this Keyword(this关键字),this is a reference to the current object(this用来引用当前对象).Refer to any member of the current object from an instance

33、method or a constructor by using this(在实例方法或构造方法内部引用当前对象的实例成员(成员变量和方法))Use this to invoke an overloaded constructor of the same class.(在构造方法内部调用本类的其它构造方法),class Circle private double radius;public Circle(double r)radius=r;public double getRadius()return radius;public void setRadius(double r)radius=r

34、;double getArea()return radius*radius*Math.PI;,49,使用this引用当前对象的实例成员,this.,隐含this,可以不写,Circle c1=new Circle(5);Circle c2=new Circle(2.5);c1.setRadius(7);/调用方法时,将this替换成c1c2.setRadius(1);/调用方法时,将this替换成c2,this.,this.,this.,this.,class Circle private double radius;public Circle(double radius)this.radiu

35、s=radius;public double getRadius()return radius;public void setRadius(double radius)this.radius=radius;double getArea()return radius*radius*Math.PI;,50,成员变量radius和局部变量radius重名,this.radius指的是成员变量radius,这里不能省略,51,class Cat String name;String color;char sex;String type;Cat(String n,String c,char s,Stri

36、ng t)name=n;color=c;sex=s;type=t;,class Cat String name;String color;char sex;String type;Cat(String name,String color,char sex,String type)this.name=name;this.color=color;this.sex=sex;this.type=type;,变量的命名应该顾名思义,这种命名方式不好,52,class Circle private double radius;public Circle(double radius)setRadius(ra

37、dius);public double getRadius()return radius;public void setRadius(double radius)if(radius=0)this.radius=radius;,this.,隐含this,53,使用this调用本类的构造方法,this调用构造方法必须是第一句,54,public class Rectangle int x,y,width,height;public Rectangle(int width,int height)this(1,1,width,height);/必须写在第一句/Rectangle(1,1,width,h

38、eight);its wrong public Rectangle(int x,int y,int width,int height)this.x=x;this.y=y;this.width=width;this.height=height;,55,class Circle double radius;final static double PI=3.14;Circle(double radius)this.radius=radius;double getArea()return PI*radius*radius;public static void main(String args),1、声

39、明静态变量或者静态方法,使用static关键字。2、有static修饰的变量/方法,称为静态变量/方法3、没有static修饰变量/方法,称为实例变量/方法(Instance variable/Instance method),56,static members,When you say something is static,it means that particular member variable or method is not tied to any particular object of that class.(静态的成员变量或方法和具体的对象无关)So even you h

40、ave never create an object of that class,you can call a static method or access a static member variable.(即使没有创建对象,也可以访问静态变量和静态方法),57,Circle c1=new Circle(5);Circle c2=new Circle(2.5);,Instance variables belong to a specific instance.实例变量(非static)属于对象,其值存储于对象的空间,必须通过对象的引用变量来访问。,double r1=c1.radius;,

41、double r2=c2.radius;,c1,radius:,5.0,c2,radius:,2.5,Circle的成员变量radius就是典型的实例变量,因为每个圆都拥有独立的半径,不同的圆半径可能不同。访问radius时,必须指明“谁的半径”,58,Circle c1=new Circle(5);Circle c2=new Circle(2.5);,Static variables are shared by all the instances of the class.static变量存储于类的公共内存,被此类的所有对象共享,既可以用对象的引用变量也可以直接用类名访问,double p1

42、=c1.PI;double p2=c2.PI;double p3=Circle.PI;,c1,c2,radius:,5.0,radius:,2.5,PI:3.14,Circle的成员变量PI就是静态的,因为所有圆的PI都是相同的值(PI值和具体的圆对象无关),没有必要在每个圆对象里都存储一份PI值,所有圆对象共享一个PI值就行了。所以静态变量可以用对象访问,最好用类名直接访问。,59,public class TestClass static int ts;int i;public static void main(String args)TestClass a=new TestClass()

43、;TestClass b=new TestClass();a.i=3;b.i=5;a.ts=12;b.ts+=5;TestClass.ts+=7;System.out.print(b.i);System.out.print(TestClass.ts);,这段程序的输出是什么?,60,public class A int m=5;static int n=3;public static void main(String args)A obj1=new A();A obj2=new A();objl.m*=2;objl.n*=4;obj2.m+=1;obj2.n+=6;System.out.pri

44、ntln(obj1.m+“,”+obj1.n+“,”+obj2.m+“,”+obj2.n);,这段程序的输出是什么?,61,public class IndentifyMyParts public static int x=7;public int y=3;public static void main(String args)IndentifyMyParts a=new IndentifyMyParts();IndentifyMyParts b=new IndentifyMyParts();a.y=5;b.y=6;a.x=1;b.x=2;System.out.println(a.y+“,”+

45、b.y+“,”+a.x+“,”+b.x);,这段程序的输出是什么?,62,public class Circle double radius;static int numberOfObjects=0;Circle()radius=1.0;numberOfObjects+;Circle(double newRadius)radius=newRadius;numberOfObjects+;double getArea()return radius*radius*Math.PI;,numberOfObjects变量的作用是什么?,63,Instance and static Methods,Inst

46、ance method can receive this.you can use this keyword in a instance method.Instance methods are invoked by an instance of the class.(实例方法可以接收this,可以在实例方法中使用this,实例方法只能通过对象来访问。)There is no this for static method!Static methods can be invoked by instance of class or by the className.(静态方法内不能使用this,静态方

47、法可以用对象来访问,但通常用类名调用),64,class Circle double radius;final static double PI=3.14;Circle(double newRadius)radius=newRadius;double getArea()/此方法依赖对象 return PI*radius*radius;public static void staticMeth()double area=PI*radius*radius;/wrong area=getArea();/wrong,不能在static方法内部直接访问实例成员(变量和方法)!,static方法不知道自己

48、被哪个对象调用!,65,实例方法和静态方法的区别,如果方法依赖于类的某个实例(对象),该方法就应该定义为实例方法,相反的,如果一个方法不依赖于类的某个实例(对象),该方法为静态方法。,class Circle double radius;final static double PI=3.14;Circle(double newRadius)radius=newRadius;double getArea()/此方法依赖对象 return PI*radius*radius;static int add(int x,int y)/此方法不依赖对象 return x+y;,Circle c1=new

49、Circle(5);Circle c2=new Circle(2.5);c1.getArea();/调用实例方法(非static)c2.getArea();/调用实例方法(非static)c1.add(2,3);/静态方法可以用对象调用c2.add(2,3);/静态方法可以用对象调用Circle.add(2,3);/最好用类名调用,66,public class Foo int i=5;static int k=2;public static void main(String args)int j=i;/1 m1();/2 public void m1()i=i+k+m2(i,k);/3 pu

50、blic static int m2(int j,int j)return(int)(Math.pow(i,j);/4,下面标号的4句,哪些有错误?,67,class Test22 float u;static float v;static void setUV(boolean f)u=fac1(f);/1 v=fac2(!f);/2 static float facl(boolean f)return f?u:v;/3 float fac2(boolean f)return f?v:u;/4,下面标号的4句,哪些有错误?,68,public class Car String carName;

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

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


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号