2013年1月12日 星期六

繼承Inheritance

[概念]
物件導向重要的概念之一

Sub Class繼承Base Class

//Base Class
public class Human{
  height
  weight
  
  walk(); 
  eat();  
}

//Sub Class
public class Ken extends Human{
  code();
  playguitar();
}

一般來說,Instance Variable、Instance Method、Class Variable、Class Method都會被繼承


[細節]
public, protected, private, package的影響

Base Class和Sub Class的Instance Variable名稱相同時會發生甚麼事

Method Overriding

[延伸]
多型Polymorphism
抽象類別Abstract Class
介面Interface

[參考]
1. O'Reilly技術短文 OO
2. 良葛格學習筆記

如有錯誤,請不吝指教

2013年1月11日 星期五

[Q1-1] Longest Plateau

[SOURCE]
名題精選百則_題1.1

[INPUT]
number set which is sorted in increment order
ex. 1,2,2,3,3,3,4,5,5,6

[OUTPUT]
find the longest plateau
ex. 3,3,3

[THINK]

[SCv1]
    //arr is sorted array
    //maxL is result length
    //maxN is result element
    int i=0,tmpL=0,tmpN=0;
    int maxL=0,maxN=0;

    for(i=0;i<10;i++){
      if(i==0){
        tmpL=1;
        tmpN=arr[i];
      }
      else{ 
        if(arr[i]==arr[i-1]){
          tmpL++;
        }
        else{
          if(tmpL>maxL){
            maxL=tmpL;
            maxN=arr[i-1];
            tmpL=1;
          }
          else{
            tmpL=1;
            tmpN=arr[i];     
          }     
        }
      }
    }

[SCv2]