26 ก.ย. 2554

Java Abstract

A.java

public abstract class A extends C{
 abstract void me();
 abstract String helloAbs();
 String str;
}

B.java

public class B extends A{
 @Override
 void me() {
  str = "Hello World ";
  System.out.println(getName());
  System.out.println(getSurename());
  System.out.println(getAge());
 }
 @Override
 String helloAbs() {
  return str+toString();
 }
}

C.java

public class C {
 private String name;
 private String surename;
 private int age;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSurename() {
  return surename;
 }
 public void setSurename(String surename) {
  this.surename = surename;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 @Override
 public String toString(){
  return getName()+" "+getSurename()+" "+getAge();
 }
}


UseAbs.java

public class UseAbs {
 public static void main(String[] args) {
  B b = new B();
  b.setName("Terry");
  b.setSurename("Harn");
  b.setAge(22);
  b.me();
  System.out.println(b.helloAbs());
 }
}
Result:
Terry
Harn
22
Hello World Terry Harn 22
 

ไม่มีความคิดเห็น:

แสดงความคิดเห็น