类的继承
public class Animal {
public Animal() {
System.out.println("无参构造");
}
public Animal(String name, int age) {
System.out.println("有参构造");
this.name = name;
this.age = age;
}
private String name;
private int age;
public void sound() {
System.out.println(String.format("它是%s,今年%s岁了", this.getName(), this.getAge()));
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Dog extends Animal{
public Dog() {
System.out.println("Dog类初始化.");
}
public Dog(String name, int age) {
super(name, age);
}
@Override
public void sound() {
System.out.println(String.format("我是%s,今年%s岁了。n 枕着打印机睡,就能打出一整夜的梦吧?t————刀刀", this.getName(), this.getAge()));
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.setName("刀刀");
dog.setAge(10);
dog.sound();
}
}
public class Cat extends Animal{
public Cat() {
System.out.println("Cat类初始化.");
}
public Cat(String name, int age) {
super(name, age);
}
@Override
public void sound() {
System.out.println(String.format("我是%s,今年%s岁了。n 点上灯笼,跟着萤火虫,在夜里,找梦。t————天猫精灵", this.getName(), this.getAge()));
}
public static void main(String[] args) {
Cat cat = new Cat();
cat.setName("天猫精灵");
cat.setAge(5);
cat.sound();
}
}
output
// dog 无参构造 Dog类初始化. 我是刀刀,今年10岁了。 枕着打印机睡,就能打出一整夜的梦吧? ————刀刀 // cat 无参构造 Cat类初始化. 我是天猫精灵,今年5岁了。 点上灯笼,跟着萤火虫,在夜里,找梦。 ————天猫精灵
super关键字
对象实例化,显示实例化调用父类构造方法,在调用子类实例化构造方法
super关键字主要是调用父类方法或属性
final关键字
使用final声明的类不能被继承
使用final声明的方法不能被子类覆盖
使用final声明的变量不能被修改,即为常量
抽象类
在Java中,含有抽象方法的类称为抽象类,同样不能生成对象
public abstract class People {
public String name;
public abstract void work();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class HR extends People {
@Override
public void work() {
System.out.println(String.format("我的职业是人力资源,我叫%s", super.getName()));
}
public static void main(String[] args) {
HR hr = new HR();
// 调用从父类继承过来的属性
hr.setName("喵七七");
hr.work();
}
}
接口
一种特殊的“抽象类”,没有普通方法,由全局常量和公共的抽象方法所组成
接口定义关键字interface
public interface Animal {
public final String label="dddd";
void say();
}
public class Dog implements Animal{
@Override
public void say() {
System.out.println("这是一只狗");
}
}
public class Cat implements Animal{
@Override
public void say() {
System.out.println("这是一只猫");
}
}
public class Test {
public static void main(String[] args) {
// 调用接口中声明的常量
System.out.println(Animal.label);
// 父类引用指向Dog子类的具体实现
Animal animal = new Dog();
animal.say();
// 更换实现
animal = new Cat();
animal.say();
}
}
// output
// 接口中声明的常量
// 这是一只狗
// 这是一只猫
对象的转型
向上转型:子类对象->父类对象 安全
向下转型:父类对象->子类对象 不安全
Dog dog = new Dog(); // 子类对象->父类对象 安全 Animal animal = (Animal) dog; // 父类对象->子类对象 不安全 Cat cat = (Cat) animal;
Exception in thread "main" java.lang.ClassCastException: com.little.Dog cannot be cast to com.little.Cat
at com.little.Test.main(Test.java:9)
转换异常 向下转型是不安全的,必须知道具体的实现类
instanceof
对象之间转换时推荐使用 instanceof判断一下,这样就可以很好的避免掉上述错误
// 父类对象->子类对象 不安全
if (animal instanceof Cat) {
Cat cat = (Cat) animal;
}
内部类
定义:在类的内部定义的类
优点:可额外使用外部类的属性
缺点:破坏类的基本结构
public class Outer {
private int a = 1;
class Inner {
public void show() {
// 可以方便的使用外部类的属性
System.out.println(a);
}
}
public void show() {
Inner inner = new Inner();
inner.show();
}
public static void main(String[] args) {
// 简单调用
Outer outer = new Outer();
outer.show();
}
}
public class TestInner {
public static void main(String[] args) {
// 外部类调用
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.show();
}
}
代码块
代码块主要就是通过{}花括号括起来的代码
主要分为 普通代码块 构造块 静态代码块三类。额外还有同步代码块
普通代码块:使用花括号括起来的代码块,作用不是很大
int i;
{
// 普通代码块
i = 5;
System.out.println("i=" + i);
}
构造块作用就是扩展构造器功能,每次实例化对象都会执行构造块里的内容
{
// 构造块
System.out.println("构造块,每次实例化都会执行.");
}
public Demo01() {
System.out.println("无参构造");
}
public Demo01(int a, int b) {
System.out.println(String.format("有参构造,a=%s,b=%s", a, b));
}
public static void main(String[] args) {
new Demo01();
new Demo01(1, 2);
}
// output
// 构造块,每次实例化都会执行.
// 无参构造
// 构造块,每次实例化都会执行.
// 有参构造,a=1,b=2
静态代码块,{}括号前加static修饰词在类加载的时候执行(仅执行一次)
static {
// 静态构造块
System.out.println("静态构造块,在类加载的时候执行,仅执行一次");
}
public Demo01() {
System.out.println("无参构造");
}
public Demo01(int a, int b) {
System.out.println(String.format("有参构造,a=%s,b=%s", a, b));
}
public static void main(String[] args) {
new Demo01();
new Demo01(1, 2);
}
// output
// 静态构造块,在类加载的时候执行,仅执行一次
// 无参构造
// 有参构造,a=1,b=2
包装类
每个基本类型都有一个对应的类,这个就是包装类
| # | 基本类型 | 包装类 |
| 1 | int | Integer |
| 2 | char | Character |
| 3 | short | Short |
| 4 | long | Long |
| 5 | float | Float |
| 6 | double | Double |
| 7 | boolean | Boolean |
| 8 | byte | Byte |
装箱拆箱
int i = 10;
// 装箱
Integer i1 = new Integer(i);
// 拆箱
i = i1.intValue();
相同类型不用这样做,会自动装箱拆箱



