AllOop.java
import java.util.*;
import face.*;//自定義的包
import love.*;//同上
import javax.swing.*;
public class AllOop {
/**abstract instanceof extends interfacce implements final super等關鍵字
* @param args
*/
public static void main(String[] args) {
oop();
}
public static void oop() {
Top top = new Top();
LiangYin ai = new LiangYin(520.1314);
ai.look();
System.out.print("Please enter the name of company: ");
String cName = input.nextLine();
System.out.print("Please enter the address of company: ");
String cAddress = input.nextLine();
System.out.print("Please count the number of employee: ");
int cPerson = input.nextInt();
Company company = new Company(cName, cAddress, cPerson);
tu(company);
Employee employee = new Employee(cName, cAddress, cPerson, 123.4, "liuwei", 25);
Employee employee1 = new Employee(cName, cAddress, cPerson, 123.4, "liuwei", 25);
System.out.println(employee.equals(employee1));
tu(employee);
paying(employee);
System.out.println(employee.toBack());
employee.doSomething();
}
//多態性,動態綁定
public static void tu(Eepay x)
{
x.output();
}
public static void paying(Pay x)
{
System.out.println(x.allPay());
}
private static Scanner input = new Scanner(System.in);
}
interface Eepay//定義接口
{
public int num();
public void output();
}
interface Pay
{
public double allPay();
}
class Company implements Eepay//實現接口
{
public Company() {}
public Company(String name, String address, int numPerson) {
this.name = name;
this.address = address;
this.numPerson = numPerson;
}
public int num() {
return (int)Math.pow(numPerson, 2);
}
public void output() {
System.out.println(name + " " + address + " " + numPerson);
}
public String toString()
{
return name + " " + address;
}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getAddress() {return address;}
public void setAddress(String address) {this.address = address;}
public int getNumPerson() {return numPerson;}
public void setNumPerson(int numPerson) {this.numPerson = numPerson;}
private String name;//公司名
private String address;//地址
private int numPerson;//公司員工數
}
final class Employee extends Company implements Eepay, Pay, Wei//final修飾符,最後的類,不能擴展的類
{
public Employee(){}
public Employee(String name, String address, int numPerson,
double priceHour, String eName, int age)
{
super(name, address, numPerson);
this.priceHour = priceHour;
this.seteName(eName);
this.setAge(age);
}
public boolean equals(Object obj)//重寫Object類的equals方法
{
if (obj == null) return false;
else
{
if (obj instanceof Employee)
{
Employee e = (Employee)obj;
return e.age == this.age && e.eName == this.eName && e.priceHour == this.priceHour;
}
}
return false;
}
public void doSomething()
{
System.out.println(eName + " " + age + " " + priceHour);
}
public String toBack()
{
return super.getAddress() + super.getName();
}
public int num()
{
return (int)(age*this.allPay());
}
public void output()
{
System.out.println(eName + " " + age + " " + priceHour);
}
public double allPay() {
double all = super.getNumPerson() * priceHour;
return all;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String geteName() {
return eName;
}
public void seteName(String eName) {
this.eName = eName;
}
private double priceHour;
private String eName;
private int age;
}
class LiangYin extends You
{
public LiangYin(){}
public LiangYin(double sun)
{
super(sun);
}
public void look()
{
System.out.println(this.getSun());
}
}
package包
Top.java
package face;
public class Top {
public Top()
{
System.out.println("Good luck!Student!");
}
}
wei.java
package face;
public interface Wei//接口單獨寫一個包時必須聲明public而且文件名與接口名一致
{
public String toBack();
}
you.java
package love;
public abstract class You//抽象類
{
public You(){}
public You(double sun) {
this.setSun(sun);
}
public abstract void look();
public double getSun() {
return sun;
}
public void setSun(double sun) {
this.sun = sun;
}
private double sun;
private static int NUM = 1;
static {
NUM = 100 * NUM;
}
}
抽象類,不允許創建對象的類,只需要在class前面加上abstract關鍵字;
在java中,一個類只能有一個超類,也就是父類,可以有多個子類,繼承的關鍵字是extends,這樣就不能像C++那樣有多繼承的出現;
而接口,恰恰可以解決將java中不能多繼承的問題,繼承接口的關鍵字是implements,創建接口的關鍵字是interface,類和接口都可以繼承多個接口。