Skip to main content
Version: java

面向对象

创建

public class Hero {
String name; //姓名
}

声明

Hero h; // 引用

赋值

// 声明并赋值
Hero h = new Hero();

// 或者声明过的直接赋值
h = new Hero();

构造方法

public class Hero {
//这个无参的构造方法,如果不写,
//就会默认提供一个无参的构造方法
public Hero(){
System.out.println("调用Hero的构造方法");
}
}

this

this代表当前对象

public class Hero {
String name; //姓名

// 带一个参数的构造方法
public Hero(String name){
//this.name代表的是属性name
this.name = name;
}

public void showInMemory(){
System.out.println("打印this看到的虚拟地址:"+this);

// 调用 Hero(String name)
this(name);
}
}

公有私有

  • private 私有的 属性
  • package/friendly/default 不写 用得不多
  • protected 受保护的 继承
  • public 公共的 被调用
自身同包子类不同包子类同包类其他类
private访问继承-no继承-no访问-no访问-no
package访问继承继承-no访问访问-no
protected访问继承继承访问访问-no
public访问继承继承访问访问

继承

在原有的基础上扩展

public class Item {
String name;
}

public class Cat extends Item{
// 在拥有名称的基础上再拥有价格属性
int price;
}

Cat cat = new Cat();
cat.name = '猫';

接口

提供一种约定

不能被实例化

interface Person {
// 必须赋初值,不能被修改
public static final int a=10;

//JDK1.8
default void sayHello(){
System.out.println("Hello World");
}

// 跑
// 修饰符必须为 public 或者不写
public void run();

abstract void method();
}

// 学生有跑的功能
class Student implements Person {

@Override
public void run() {
System.out.println(" run");
}
}

接口其他

// 扩展接口
interface Man extends Hello {
String getName();
}

// 实现多个接口
class Student implements Person, Hello { // 实现了两个interface
...
}

多态

// 拥有名字
public class Item {
String name;
}

// 跑
public interface Action {
void run();
}

// 猫
public class Cat extends Item implements Action {
// 覆盖父类方法方法
public void run(){
System.out.println(name+"在跑");
}
}

// 人
public class Person extends Item implements Action {
public void run(){
System.out.println(name+"在跑");
}
}

// 狮子
public class Lion {
public void fear(Action a) {
a.run();
}

public static void main(String[] args) {
Item i1= new Cat();
i1.name='猫';
Item i2 = new Person();
i2.name='人';

Lion lion = new Lion();
lion(i1);
lion(i2);
}
}

抽象类

"空"方法

单继承, 不能被实例化

// 抽象类 abstract 关键字
// 可以没有抽象方法
public abstract class Hero {
private int number;

// 必须被声明为抽象类
// 子类实现抽象方法,关键字extends
public abstract void attack();

public String getName()
{
return name;
}
}

内部类

非静态内部类, 静态内部类

public class Item{
private String name; // 姓名

protected static int age = 1;

// 私有方法
private void out_method(){
System.out.println("out_method");
}

// 私有静态方法
private static void out_static_method(){
System.out.println("out_static_method");
}

// 非静态内部类,只有一个外部类对象存在的时候,才有意义
class Person{
public void demo(){
// 这里外部所有方法和属性都可以访问
out_static_method();
System.out.println(name + "-Person !");
}
}

// 静态内部类
static class Cat{

public void demo_cat(){
// 这里不能访问方法 out_method() 属性 name
System.out.println(age + "-Cat age !");
}
}

public static void main(String[] args) {
Item it = new Item();
it.name = "Out";
Person per = it.new Person();
// 结果: Out-Person!
per.demo();

// 实例化静态内部类
Item.Cat cat = new Item.Cat();
cat.demo_cat();
}

}

匿名类

public abstract class Item{

public abstract void out_method();

public static void main(String[] args) {

// 匿名类指的是在声明一个类的同时实例化它
Item anonymous = new Item(){
public void out_method(){
System.out.println("anonymous out_method");
}
};

anonymous.out_method();
}
}

本地类

public class Person{
String name; //姓名

public static void main(String[] args) {

//本地类有了自定义的类名
class Man extends Person{
public void run() {
System.out.println( name+ " run");
}
}

Man h =new Man();
h.name ="man";
h.run();
}
}

关键字

super

super("123"); // 显式调用父类带参的构造方法

super.age; // 调用父类属性

super.getAge(); // 调用父类方法

final

// 不能被继承
public final class Item{
// 不能被重写
public final void demo(){}

public static void main(String[] args) {
// 只能赋值一次
final int age;
age = 12; // age = 13;

// 引用只有1次指向对象的机会
final Item it;
}
}