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,类和接口都可以继承多个接口。