Java Tutorial/Class Definition/Class Object

Материал из Java эксперт
Перейти к: навигация, поиск

Assignment with objects is a bit tricky.

   <source lang="java">

class Number {

 int i;

} public class MainClass {

 public static void main(String[] args) {
   Number n1 = new Number();
   Number n2 = new Number();
   n1.i = 9;
   n2.i = 47;
   System.out.println("1: n1.i: " + n1.i + ", n2.i: " + n2.i);
   n1 = n2;
   System.out.println("2: n1.i: " + n1.i + ", n2.i: " + n2.i);
   n1.i = 27;
   System.out.println("3: n1.i: " + n1.i + ", n2.i: " + n2.i);
 }

}</source>



1: n1.i: 9, n2.i: 47
2: n1.i: 47, n2.i: 47
3: n1.i: 27, n2.i: 27


Class Object

  1. All the classes defined are subclasses by default.
  2. Object is a superclass of every class.
  3. The inheritance happens automatically.
  4. Your classes will inherit members from the class Object.

MethodPurposetoString()returns a string describing the current objectequals()compares two objectsgetClass()returns a Class that identifies the class of the current object.hashCode()calculates a hashcode for an objectnotify()wakes up a thread associated with the current object.notifyAll()wakes up all threads associated with the current object.wait()causes a thread to wait



   <source lang="java">

class Dog{

 public Dog(String aType){
 }

} public class MainClass{

 public static void main(String[] a){
   Dog d = new Dog("a");
   Class objectType = d.getClass();             
   System.out.println(objectType.getName());      
 }

}</source>



Dog


Demonstrate Run-Time Type Information.

   <source lang="java">

class X {

 int a;
 float b;

} class Y extends X {

 double c;

} class MainClass {

 public static void main(String args[]) {
   X x = new X();
   Y y = new Y();
   Class<?> clObj;
   clObj = x.getClass(); 
   System.out.println("x is object of type: " + clObj.getName());
   clObj = y.getClass(); 
   System.out.println("y is object of type: " + clObj.getName());
   
   clObj = clObj.getSuperclass();
   System.out.println("y"s superclass is " + clObj.getName());
 }

}</source>





Getting java.lang.Class: Information about your object

   <source lang="java">

public class MainClass {

 public static void main(String[] a) {
   String country = "Canada";
   Class myClass = country.getClass();
   System.out.println(myClass.getName());
 }

}</source>



java.lang.String


Storing a reference to the String object as type Object

A variable of type Object can store a reference to an object of any class type.



   <source lang="java">

public class MainClass {

 public static void main(String[] a) {
   String saying = "A stitch in time saves nine.";
   Object str = saying;
   System.out.println(str);
   System.out.println(str.getClass().getName());
 }

}</source>



A stitch in time saves nine.
java.lang.String


The "Class" class also brings the possibility of creating an object without using the new keyword.

  1. The static forName method creates a Class object of the given class name.
  2. The newInstance method creates a new instance of a class.



   <source lang="java">

public class MainClass {

 public static void main(String[] a) {
   Class klass = null;
   try {
     klass = Class.forName("java.lang.String");
   } catch (ClassNotFoundException e) {
   
   }
   
   if (klass != null) {
     try {
       // create an instance of the Test class
       String test = (String) klass.newInstance();
     } catch (IllegalAccessException e) {
     } catch (InstantiationException e) {
     }
   }
 }

}</source>