object

1.     概念:Object类是类层次结构的根类,所有类都以Object类作为自己的超类(父类)。所有对象(包括数组)都可以实现这个类的方法。

*几个基本方法(在后面会具体讲到,这里只是提提)

1.public int hashcode( ) 返回该对象的哈希码只值(通过哈希算法,对照哈希表求出哈希值,也是地址,但是这的地址不是真正意义上的地址。)

2.public final class getclass( )返回当前Object类运行实类。(Class类中有一个方法:

 publicString getName()以 String 的形式返回此Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称。)返回的是当前这个类的全路径名称。

2.     public StringtoString( )返回当前当前字符串的表示(建议重写),但若要想直接返回该对象的名称,返回该对象成员变量的值,就必须要重写toString 方法。

例:进行重写

packageinterpackage;

 

class gzx{

   public static void main(String[] args) {

    Student s=new Student();

     System.out.println(s.toString());

   }

}

class Student{

   String name="bingo";

   int age=20;

publicStudent() {

   super();

}

public void setName(String nameparm) {

   this.name=nameparm;

}

publicString getName() {

   return this.name;

}

public void setage(int ageparm) {

   this.age=ageparm;

}

public int getage() {

   return this.age;

}

//重写的快捷键是Alt+shift+s--à+s

@Override

publicString toString() {

   return "Student[name=" + name + ", age=" + age + "]";

}

}

    

注:在后面会提到,但是这里要用那就是Integer类中的一个方法,public static String toHexString(int i) 将这个int型数据转换成一个十六进制的字符串。

例:各种返回值的比较

Student s=new Student( );

1.System.out.println(“s”+s);

2.System.out.println(“s”+s.toString( ));

3.System.out.println(s.getclass( ).getName( )+”@”+Integer.toHexString(s.hashcode( ) );

这三种输出结果都相同,都是当前对象的地址。

3.protected void finalize( ) throwsThrowable

当垃圾回收器确定该对象不会被更多的引用时,垃圾回收期就会调用这个方法,但是何时调用还不确定。(在以后会讲,在System类中有一个方法,运行垃圾回收器 public void gc(),此方法追中调用的就是finalize方法。)

4.Protected Objectclone( )创建并返回此对象的一个副本,注意此方法实施特定的复制方法,但是该对象的类不能够实现接口cloneable则会抛出 cloneNotSupporedtException

packageinterpackage;

 

class gzx{

   public static void main(String[] args) throws CloneNotSupportedException {//该异常是编译时期异常

      //创建s1这个学生对象

      Student s1 = new Student() ;

      s1.setName("高圆圆") ;

      s1.setage(27) ;

     

      System.out.println(s1.getName()+"---"+s1.getage());

     

      //复制s1这个对象

      Object obj = s1.clone() ;

      //向下转型:

      Student s2 = (Student) obj;

      System.out.println(s2.getName()+"---"+s1.getage());

      System.out.println("----------------------");

     

      //没有讲clone方法之前:

      Student s3 = s1; //s1地址值赋值s3对象,s3指向s1的堆内存中的地址值

      System.out.println(s3.getName()+"---"+s3.getage());

   }

}

 

class Student implements Cloneable{

   int age;

   String name;

   public Student() {

      super();

   }

   public Student(String nameparm,int ageparm) {

      super();

      this.name=nameparm;

      this.age=ageparm;

   }

   public void setName(String nameparm) {

      this.name=nameparm;

   }

   public String getName() {

      return this.name;

   }

   public void setage(int ageparm) {

      this.age=ageparm;

   }

   public int getage() {

      return this.age;

   }

   @Override

   protected Object clone() throws CloneNotSupportedException {

      // TODOAuto-generated method stub

      return super.clone();

   }

}

注意:当我们要完成clone方法的时候,系统一定会抛出CloneNotSupportedException ,此时就算不完成Cloneable接口,系统也不会发生呢个报错,,但是在后面一定要进行Clone方法的重写。

packageinterpackage;

 

class gzx{

   public static void main(String[] args) throws CloneNotSupportedException {//该异常是编译时期异常

      //创建s1这个学生对象

      Student s1 = new Student() ;

      s1.setName("高圆圆") ;

      s1.setage(27) ;

     

      System.out.println(s1.getName()+"---"+s1.getage());

     

      //复制s1这个对象

      Object obj = s1.clone();

      //向下转型:

      Student s2 = (Student) obj;

      System.out.println(s2.getName()+"---"+s1.getage());

      System.out.println("----------------------");

     

      //没有讲clone方法之前:

      Student s3 = s1; //s1地址值赋值s3对象,s3指向s1的堆内存中的地址值

      System.out.println(s3.getName()+"---"+s3.getage());

   }

}

 

class Student implements Cloneable {

   int age;

   String name;

   public Student() {

      super();

   }

   public Student(String nameparm,int ageparm) {

      super();

      this.name=nameparm;

      this.age=ageparm;

   }

   public void setName(String nameparm) {

      this.name=nameparm;

   }

   public String getName() {

      return this.name;

   }

   public void setage(int ageparm) {

      this.age=ageparm;

   }

   public int getage() {

      return this.age;

   }

   @Override

   protected Object clone() throws CloneNotSupportedException {

      // TODOAuto-generated method stub

      return super.clone();

   }

  

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"In this thoroughly revised edition, Jos and Anneke offer a concise, pragmatic, and pedagogic explanation of the Object Constraint Language (OCL) and its different applications. Their discussion of OCL's potential role in Model Driven Architecture (MDA) is timely and offers great insight into the way that UML can be taken to the next level of automated software development practice. I highly recommend this book to anyone who is looking to get the most out of UML" -Shane Sendall, Ph.D.Senior ResearcherSwiss Federal Institute of Technology in Lausanne The release of Unified Modeling Language (UML) 2.0 places renewed emphasis on the Object Constraint Language (OCL). Within UML, OCL is the standard for specifying expressions that add vital information to object-oriented models and other object-modeling artifacts. Model-Driven Architecture (MDA) relies on OCL to add the level of programming detail necessary to enable Platform-Specific Models (PSM) to communicate with Platform-Independent Models (PIM). This book is a practical, accessible guide to OCL for software architects, designers, and developers. Much care has been taken during the redesign of OCL to ensure that the syntax remains readable and writable by the average software modeler. The Object Constraint Language, Second Edition, utilizes a case study to show how to exercise these compact but powerful expressions for maximum effect. This newly updated edition also Explains why OCL is critical to MDA-and why UML alone is not enough Introduces an SQL-like syntax to OCL Defines the new language constructs of OCL 2.0 Demonstrates how OCL can be incorporated into code Using a combination of UML and OCL allows developers to realize the effective, consistent, and coherent models that are critical to working with MDA. The authors' pragmatic approach and illustrative use of examples will help application developers come quickly up to speed with this important object-modeling method--and will serve as a ready reference ther

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值