askldjf

例子1

/**
 * 
 */
package com.atguigu.java;

/*
 * 
 * 
 *
 */
public class AbstractTest {
	public static void main(String[] args) {
		
		//一旦Person类抽象了,就不可实例化
		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类抽象了,就不可实例化
		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{
	
	//虽然Person抽象了,但在子类中可以将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类抽象了,就不可实例化
		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{
	
	//虽然Person抽象了,但在子类中可以将Person实例化用
	public Student(String name,int age){
		super(name,age);
	}
	
	//现在46行代码错了,有两种方法:1.抽象类中的方法在子类中进行重写 2.将子类也设置为抽象类,详见46
//	public void eat(){
//		System.out.println("学生多吃有营养的食物");
//	}
	
}

例子5

Employee父类

/**
 * 
 */
package com.atguigu.exer1;

/**
 * @Descripton
 * @author wsf  Email:2181821498@qq.com
 * @version
 * @date 2021年10月18日下午7:14:29
 *
 */

//此时这个类要改成抽象的类
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;
	}
	
	//设置一个抽象的方法 abstract
	public abstract void work();
	
}

Manager子类

/**
 * 
 */
package com.atguigu.exer1;

/**
 * @Descripton
 * @author wsf  Email:2181821498@qq.com
 * @version
 * @date 2021年10月18日下午7:19:07
 *
 */
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;

/**
 * @Descripton
 * @author wsf  Email:2181821498@qq.com
 * @version
 * @date 2021年10月18日下午7:24:23
 *
 */
public class CommonEmployee extends Employee {


	public void work() {
		System.out.println("员工在一线车间生产产品");
	}

}

测试类

/**
 * 
 */
package com.atguigu.exer1;

/**
 * @Descripton
 * @author wsf  Email:2181821498@qq.com
 * @version
 * @date 2021年10月18日下午7:24:23
 *
 */
public class CommonEmployee extends Employee {


	public void work() {
		System.out.println("员工在一线车间生产产品");
	}

}

例子6

/**
 * 
 */
package com.atguigu.java;

/**
 * @Descripton
 * @author wsf  Email:2181821498@qq.com
 * @version
 * @date 2021年10月18日下午8:15:38
 *
 */
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);
			}
		}
		
	}
	
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值