Java - Class 090821

- Referred to wikipedia.org

 

Declaration

 

class classname extends superclassname

{

    type         instance-variable1;

    ......

    type         instance-variableN;

 

    type         methodname1(parameter-list)

    { method-body; }

......

type methodnameN(parameter-list)

    { method-body; }

}

 

Class of Object is the root of all classes.

 

 

Class Relationships

 

Use – when a class uses the public methods of another class

Inheritance – when a class is derived from another class.

Composition – when a class is composed of other classes.

 

When we design an object-oriented program:

ž            the first task is to identify the object (classes)

ž            the second to identify the objects’ responsibilities (methods). 

A popular method to establish the beginnings of a class structure is:

ž            to study the problem description and isolate the nouns, these will often become classes.

ž            the verbs will often become methods.

 

 

This, Super

 

public class testClassOne {

      

       int i;

      

       public testClassOne() {

              i = 10;

              System.out.println("I am construction method: testClassOne(); i=");

       }

      

       /* overloading method testClassOne() */

       public testClassOne (int value) {

              this.i = value;        // this is pointer pointing to ClassOne

              System.out.println("I am construction method: testClassOne(int value); i=");

       }  

      

       public void Add_i(int j)  {

              i = i + j;

              System.out.println("I am public method Add_i(int j) of class: testClassOne; i=" + i);

       }

}

 

 

/*     These two demo show following key techical points:

       1. class inheritence

       2. method overloading: testClassOne(int Value) overload testClassOne()

       2. method overriding: child's method Add_i() override corresponding method Add_i() of parent       */

 

/*     if testClassOne.class and testClassOneChild.class are placed in the same directory,

       they are part of the same package and they can access each other.

       Else, import package is needed.        */

//import testClassOne;

 

/* testClassOneChild inherit testClassOne, and override the Add_i() of his parent*/

public class testClassOneChild extends testClassOne {

       public void Add_i(int j) {

              i = i+(j/2);

              System.out.println(i);                                // i = 15

              super.Add_i (j);                                        // super point to testClassOne and do i=i+j again

              System.out.println("I am method Add_i() of testClassOneChild; i="+i);

       }

      

       public static void main(String args[]){

              testClassOneChild objClassOne;

              testClassOne objClassTwo;

              objClassOne = new testClassOneChild();  // call parent construction method without parameter: testClassOne

              objClassOne.Add_i(10);                                   // call method Add_i(int j) and i=15

              System.out.println(objClassOne.i);           

             

              System.out.println('/n'+"call construction method of parent with parameter 10");

              objClassTwo = new testClassOne(10);              // directly call parent's construction

              objClassTwo.Add_i(10);

              System.out.println(objClassTwo.i);

       }

}

 

 

Visibilities of variable and method

 

ž            public - it can be used by any object derived from this and other class

ž            protected实例变量和成员函数只能被其子类调用

ž            private实例变量和成员函数只能在本类里调用

ž            friendly 缺省的,如果没有定义任何访问控制,实例变量或函数缺省定义成friendly,意味着可以被本包里的任意对象访问,但其它包里的对象不可防问。

ž            static - any of the classes other members can access it. 静态成员函数和变量。有的时候,你创建一个类,希望这个类的所有实例都公用一个变量。也就是说,所有这个类的对象都只有实例变量的同一个拷贝。

 

class stTest { static int number=50; }

所有从stTest类创建的对象的number变量值都是相同的。无任在哪个对象里改变了number的值,所有对象的number都跟着改变。同样的,可以定义static成员函数,但这个成员函数不能访问非static函数和变量。

Class stTest{

  static int number=50;

  int localvalue;

  static void add_local()

  {

    localvalue++;//没有运行

  }

  static void add_static();

  {

    number++;//运行

  }

}

 

 

Polymorphism

Type polymorphism /pɔliˈmɔ:fizəm/ in object-oriented programming is the ability of one type, A, to appear as and be used like another type, B.  In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B.  In weakly typed languages types are implicitly polymorphic.

 

Polymorphism is not the same as method overloading or method overriding.  

ž            Polymorphism (多态) is only concerned with the application of specific implementations to an interface or a more generic base class.  

ž            Method overloading (函数重载) refers to methods that have the same name but different signatures inside the same class.  

ž            Method overriding (函数覆盖) is where a subclass replaces the implementation of one or more of its parent's methods.

 

Inheritance combined with polymorphism allows class B to inherit from class A without having to retain all features of class A; it can do some of the things that class A does differently. This means that the same "verb" can result in different actions as appropriate for a specific class.

 

If a Dog is commanded to speak(), it may emit a bark, while if a Pig is asked to speak(), it may respond with an oink. Both inherit speak() from Animal, but their subclass methods override the methods of the superclass, known as overriding polymorphism.

 

interface Animal
{
    String getName();
    String talk();
}
 
abstract class AnimalBase implements Animal
{
    private final String name;
    protected AnimalBase(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}
 
class Cat extends AnimalBase 
{
    public Cat(String name) {
        super(name);
    }
    public String talk() {
        return "Meowww!";
    }
}
 
class Dog extends AnimalBase 
{
    public Dog(String name) {
        super(name);
    }
    public String talk() {
        return "Arf! Arf!";
    }
}
 
public class TestAnimals
{
    // prints the following:
    // Missy: Meowww!
    // Mr. Bojangles: Meowww!
    // Lassie: Arf! Arf!
    public static void main(String[] args) {
        Animal[] animals = {
            new Cat("Missy"),
            new Cat("Mr. Bojangles"),
            new Dog("Lassie")
        };
        for (Animal a : animals) {
            System.out.println(a.getName() + ": " + a.talk());
        }
    }
}

 

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run time (this is called late binding or dynamic binding).

 

class A {

    void callme( )

    { System.out.println(" callme method of class A"); }

}

 

class B extends A {

void callme( )

{

    System.out.println(" callme method of class B");

}

}

class Dispatch {

    public static void main(String args[])

{

    A a = new B( );

        a.callme( ); // Java complier rectify class A has callme method,

// while for ‘a’ is an instance of class B,

// callme of class B will be called as opposed class A

    }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值