วันอาทิตย์ที่ 11 มีนาคม พ.ศ. 2555

mypg


package mypg;

import java.util.*;

public class Geometric {
private String color = "white";
private Date dateCreated;
public Geometric()
{
dateCreated = new Date();

}
public Geometric (String color)
{
this.color = color;
}
public void setColor(String color)
{
this.color = color;
}
public String getColor()
{
return this.color;
}
public Date getDate()
{
return dateCreated;
}
}
+++++++++++++++++++++++++++++++
package mypg;

public class Circle extends Geometric{
private double radius;

public Circle (double radius)
{
this.radius = radius;

}
public Circle(double radius,String color)
{
this.radius = radius;
setColor(color);
}
public double getArea()
{
return radius * radius * Math.PI;
}
}
+++++++++++++++++++++++++++++++++
package mypg;

public class Test {

public static void main(String[] args) {
Geometric geo1 = new Geometric();
System.out.println(geo1.getColor()+" ; "+geo1.getDate());
Circle cir1 = new Circle(4);
cir1.setColor("red");
System.out.println(cir1.getColor()+ " ; " +cir1.getDate() + " ; " +cir1.getArea());

}

}


Polymorphism


nแปลว่า หลายรูปทรง แปลงสภาพได้
Example
class CustomerData{
   protected void CustomerPhone(){
        System.out.println(“02 123 456”);
   {
   protect void ClearAll(){
        System.out.println(“Delete All Data”);
   }
}
+++++++++++++++++++++++++++++++++++++++++ๅๅๅๅ
เวลาเรียกใช้
class CustomerData2 extends CustomerData {
   protected void CustomerPhone(){
         super.CustomerPhone();
         System.out.println(“02 123 456 7”);
   {
   protect void ClearAll(){
   }
}


Inheritance


nแปลว่า การสืบสันดาน หรือ การสืบมรดก
nต่อยอดงานใหม่ จากงานเก่าไม่ต้องเริ่มจากศูนย์
nภาษา OOP สนับสนุน Inheritance เสมอ
Example
class Foo{
   public void WriteFoo(){
      System.out.println(“Foo”);
   }
}

class Bar extends Foo{
   public void WriteBar(){
      System.out.println(“Bar”);
   }
}

+++++++++++++++++++++++++++++++++++++++++ๅ
เวลาเรียกใช้
class Program{
   public static void main (String[] args){
      Bar myBar = new Bar();
      myBar.WriteFoo();
      myBar.WriteBar();
   }



Overloaded Constructor


nเป็นคอนสตรัคเตอร์กำหนดเอง
nคอนสตรัคเตอร์ทุกตัวชื่อเหมือนกับคลาส
nทำการรับพารามิเตอร์ไม่เหมือนกัน

class Calculator{
  Calculator(int x){
  return x + x;}
   Calculator(int x, int y){
  return x + y;}  
   Calculator(int x, int y, int z){
  return x + y + z;}
}

เวลาเรียกใช้คอนสตรัคเตอร์
class TestCalculator{
  public static void main(String[] args) {
  Calculator a = new Calculator();
  System.out.println(a.Calculator(1));
 
  Calculator b = new Calculator();  
  System.out.println(b.Calculator(1, 2));
  Calculator c = new Calculator();
  System.out.println(c.Calculator(1, 2, 3));
  }
}


โอเวอร์โหลดเมธอด


public class TestOver1 {
  public static int add() {
  return 0;       }
  public static int add(int x) {
  return x;      }
  public static int add(int x, int y)   {
  return x+y;               }
  public static int add(int x, int y, int z) {
  return x+y+z;                      }


เวลาเรียกใช้เมธอด
  public static void main(String[] args)                   
{
  System.out.println("Sum = "+add());
  System.out.println("Sum = "+add(3));
  System.out.println("Sum = "+add(1,2));
  System.out.println("Sum = "+add(1,2,3)); }
}
Test_java2

วันจันทร์ที่ 6 กุมภาพันธ์ พ.ศ. 2555

TV

package
java_1;


public
class TV {

private int chanel = 1;//1-10

private int volume = 1;//0-5

private boolean status = false;// By default TV is off

private String brand = "";

public static int count = 0;


TV(){

count++;

brand = "Sony";

}

TV(String newBrand){

count++;

brand = newBrand;

}

TV(String newBrand,
int newchanel){

count++;

brand = newBrand;

chanel = newchanel;

}

public String getBrand(){

return brand;

}

public void turnOn(){

//Mutator of status variable

status = true;

}

public void turnOff(){

//Mutator of status variable

status = false;

}

public boolean getStatus(){

//Accessor of status variable

return status;

}

public void chanelUp(){

chanel++;

if(chanel > 10){

chanel = 1;


}

}

public void chanelDown(){

chanel--;

if(chanel < 1){

chanel = 10;

}

}

public int getChanel(){

if(status == false)

{

System.
out.println("Please TurnOn TV");

return 0;

}

else

return chanel;

}

public void volumeUp(){

if(volume < 5)

volume++;

else

System.
out.println("Maximum");

}

public void volumeDown(){

if(volume > 0)

volume--;

else

System.
out.println("Minimum");


}

public int getVolume(){

return volume;

}

public boolean equalsTV(TV o){

if(o.getChanel() != this.getChanel())

return false;

if(o.getStatus() != this.getStatus())

return false;

if(o.getVolume() != this.getVolume())

return false;

return true;

}


}