Java面向对象编程2.1.ppt

  1. 1、本文档共76页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Java面向对象编程2.1

Create Object Declare: Point p;//只是声明引用,不在内存中为对象分配地址空间,而是分配一个引用空间; Instantiation: 运算符new为对象分配内存空间,它调用对象的构造方法,返回引用;一个类的不同对象分别占据不同的内存空间。 Create: 执行构造方法,进行初始化;根据参数不同调用相应的构造方法。 Creating objects of a class Creating objects of a class Automatic garbage collection The object does not have a reference and cannot be used in future. The object becomes a candidate for automatic garbage collection. Java automatically collects garbage periodically and releases the memory used to be used in the future. the compiler will complain because it cant find a method called getSalary in the class Person—at this point in the code, the compiler only knows the person is a Person. person is change the reference person into an Employee reference with a cast. person is Employee employee = (Employee) person; double salary = employee.getSalary(); // or double salary = ((Employee) person). getSalary(); is invoke the getSalary method We can cast the reference person to an Employee reference because person really points to an Employee object. If it did not and we ran the code, the cast would be illegal and the Java virtual machine (JVM) would throw a ClassCastException. Person person = new Person (Joe Smith, 100 Main Ave); person person 1 Name=“Joe” Address=“aa” = is is is double salary = ((Employee) person).getSalary();// illegal This code will compile, but when it is executed, the JVM will throw a ClassCastException because, in this example, the reference person does not point to an Employee object. The instanceof Operator two operands: an object reference , a class name object instanceof ClassX true: if the specified object is an instance of the specified class if object is an instance of a ClassX subclass if object is an instance of any class that has ClassX as an ancestor false:If the left-hand operand is null Note: the instanceof operator cannot be used to determine the type of a p

您可能关注的文档

文档评论(0)

xcs88858 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8130065136000003

1亿VIP精品文档

相关文档