例子1
package com.atguigu.java;
public class AbstractTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.eat();
}
}
abstract class Person{
String name;
int age;
public Person(){
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void eat(){
System.out.println("人吃饭");
}
public void walk(){
System.out.println("人走路");
}
}
class Student extends Person{
}
例子2
package com.atguigu.java;
public class AbstractTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.eat();
}
}
abstract class Person{
String name;
int age;
public Person(){
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void eat(){
System.out.println("人吃饭");
}
public void walk(){
System.out.println("人走路");
}
}
class Student extends Person{
public Student(String name,int age){
super(name,age);
}
}
例子3
package com.atguigu.java;
public class AbstractTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.eat();
}
}
abstract class Person{
String name;
int age;
public Person(){
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public abstract void eat();
public void walk(){
System.out.println("人走路");
}
}
abstract class Student extends Person{
public Student(String name,int age){
super(name,age);
}
}
例子5
Employee父类
package com.atguigu.exer1;
public abstract class Employee {
private String name;
private int id;
private double salary;
public Employee(){
super();
}
public Employee(String name, int id, double salary) {
super();
this.name = name;
this.id = id;
this.salary = salary;
}
public abstract void work();
}
Manager子类
package com.atguigu.exer1;
public class Manager extends Employee{
private double bonus;
public Manager(double bonus) {
super();
this.bonus = bonus;
}
public Manager(String name, int id, double salary, double bonus) {
super(name, id, salary);
this.bonus = bonus;
}
public void work() {
System.out.println("管理员工,提高公司的运行效率");
}
}
CommonEmployee子类
package com.atguigu.exer1;
public class CommonEmployee extends Employee {
public void work() {
System.out.println("员工在一线车间生产产品");
}
}
测试类
package com.atguigu.exer1;
public class CommonEmployee extends Employee {
public void work() {
System.out.println("员工在一线车间生产产品");
}
}
例子6
package com.atguigu.java;
public class TemplateTest {
public static void main(String[] args) {
SubTemplate t = new SubTemplate();
t.spendTime();
}
}
abstract class Template{
public void spendTime(){
long start = System.currentTimeMillis();
code();
long end = System.currentTimeMillis();
System.out.println("花费的时间为:"+(end-start));
}
public abstract void code();
}
class SubTemplate extends Template{
public void code(){
for(int i=2; i<=1000; i++){
boolean isFlag=true;
for(int j=2; j<=i/j; j++){
if(i%j==0){
isFlag=false;
break;
}
}
if(isFlag){
System.out.println(i);
}
}
}
}