一个类是由确定和不确定的部分组成的,那么可以将不确定的部分设置为抽象的,让子类重写方法,完成不确定的部分。
(尽量不要修改源代码)
1、提高了代码的复用性
2、提高了代码的扩展性
计算一段程序执行的时间
提示:System.currentTimeMillis( ),能够获取系统当前的时间,单位是毫秒
代码示例:
public class Demo01 {
public static void main(String[] args) {
SubTime g = new SubTime();
g.getTime();
}
}
abstract class GetTime{
public void getTime() {
//获取系统当前时间
long a = System.currentTimeMillis();
fun();
//获取程序执行完毕时候的当前时间
long b = System.currentTimeMillis();
long c = b - a;
System.out.println("程序执行时间:"+c+"(毫秒)");
}
public abstract void fun();
}
//使用继承
class SubTime extends GetTime{
public void fun() {
for(int x= 0 ;x < 20000 ; x++) {
System.out.println("ddd");
}
}
}
在一个类中使用了另一个类的对象作为成员变量,在类中要定义相对应的set和get方法
而后使用对象调用所属类中的方法,在之后主函数中,要给类中的对象一个具体的实体。
* 1.人类 吃
* 2.男人类 姓名 性别 吃肉 有女朋友,介绍女朋友是谁
* 3.女人类 姓名 性别 吃素 有男朋友,介绍男朋友是谁
* 4.男人挣钱给女朋友花(接口实现)
Person类:
abstract class Person{
private String name;
private String sex;
public Person() {
}
public Person(String name,String sex) {
this.name = name;
this.sex = sex;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public abstract void introduce();
public abstract void eat();
}
class Man extends Person implements EarnMoney{
//定义一个成员变量,是女人类的对象
private WoMan gf ;
public Man() {
}
public Man(String name,String sex) {
super(name,sex);
}
public void introduce() {
System.out.println("我有女朋友");
}
//给gf写一对set和get方法
public void setgf(WoMan gf) {
this.gf = gf;
}
public WoMan getgf() {
return gf;
}
public void eat() {
System.out.println("我吃肉");
}
//实现接口中的抽象方法
public void earnMoney() {
System.out.println("我可以挣钱");
}
//介绍女朋友的方法
public void sayHi() {
System.out.println("我是:"+this.getName());
System.out.println("我的女朋友是:"+gf.getName());//不给他值,这里的gf是null的,要重新赋值
}
public void hhh() {
System.out.println("sdfkhsdfhis");
}
}
class WoMan extends Person{
private Man bf;
public WoMan() {
}
public WoMan(String name,String sex) {
super(name,sex);
}
public void setbf(Man bf) {
this.bf =bf;
}
public Man getbf() {
return bf;
}
public void introduce() {
System.out.println("我是:"+this.getName());
System.out.println("我男朋友是:"+bf.getName());
}
public void eat() {
System.out.println("我吃素");
}
public void sayhi() {
System.out.println("我是:"+this.getName());
System.out.println("我男朋友是:"+bf.getName());
}
public void zhengRong(String body) {
System.out.println("我是:"+this.getName());
this.sayhi();
System.out.println("我"+body);
}
}
public class Test {
public static void main(String[] args) {
Man man = new Man("王宁","男");
WoMan woMan = new WoMan("高圆圆","女");
//确定男生女朋友是哪个
man.setgf(woMan);
man.sayHi();
//确定女生男朋友是哪个
woMan.setbf(man);
woMan.sayhi();
//通过男的类的对象,能不能获取到女的类的对象
//男人的女朋友去、、
WoMan gf =man.getgf();
gf.zhengRong(" ");//获取女生的对象,使用类中所有方法
//让男的去挣钱
//让女人类的对象获取bf
//女人的男朋友去赚钱Man bf = bf
woMan.getbf().earnMoney();
}
}
在程序执行的过程中,保证只有一个该类的对象
举例理解:比如网易云播放器,他可以在页面播放,也可以在后台播放,也可以在锁屏状态下播放,这就是单例的体现,保证只有一个播放器对象
1、保证外界不能建立对象
2、自己建立私有对象
3、对外暴露公共的访问方式
public class A {
//创建单例对象
private A() {
}
private static A a = new A();
//对外暴露访问的方法
public static A getInstance() {
return a;
}
}
private B() {
}
private static B b = null;
public static B getInstance() {
if(b==null) {
b = new B();
return b;
}
return b;
}
饿汉式:类一旦加载就会调用建立对象的成员变量,建立对象
懒汉式:只有在建立对象的方法被调用的时候才对象才建立,有延迟加载对象的特点