1,封装-保证数据安全-属性私有化
,2,多态-需要继承做基础,多种形态,重载
调用方法时,先找父类,没有会报错,有则会继续去子类中找,若有则执行子类方法,若无则执行父类方法
,3,设计模式 -单例模式(懒汉式,饿汉式)
一个类最多只允许创建一个对象
,4,构造函数私有
oracle – 神域
悲观性检测
==
equals
public class Pet {
public String name;
public void follow(){
System.out.println("奔跑");
}
}
public class Eagle extends Pet{
public void follow(){
System.out.println("飞快跑");
}
}
public class Cat extends Pet{
public void follow(){
System.out.println("慢跑");
}
}
public class Dog extends Pet{
public void follow(){
System.out.println("快跑");
}
}
public class Hero {
public String name;
public Pet pet;
public Hero(String name) {
this.name = name;
}
public Hero(String name, Pet pet) {
super();
this.name = name;
this.pet = pet;
}
public void run(){
System.out.println(this.name+"跑");
pet.follow();
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Pet p = new Cat();
Hero h = new Hero("蓝精灵",p);
h.run();
}
}
蓝精灵跑
慢跑
5,包装类
byte - Byte
short - Short
int - Integer
long - Long
float - Float
double - Double
char - Character
boolean- Boolean
6,instanceof:关键字 判断对象是否是该类实例
7,类之间的关系:继承
关联(聚合,组合):一个类作为另一个类属性存在
依赖:一个类作为另一个类方法的参数
静态块:static{} 类加载时被调用执行
非静态块:创建对象时
Integer Scanner
public class JingTai {
{
System.out.println("父类非静态块");
}
static{
System.out.println("父类静态块");
}
public JingTai(){
System.out.println("父类构造函数");
}
}
public class ZiLei extends JingTai {
{
System.out.println("子类非静态块");
}
static{
System.out.println("子类静态块");
}
public ZiLei(){
System.out.println("子类构造函数");
}
}
public class Test2 {
public static void main(String[] args) {
JingTai j = new ZiLei();
}
}
父类静态块
子类静态块
父类非静态块
父类构造函数
子类非静态块
子类构造函数