0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

第3

Posted at

1.static(静态)

①修饰成员变量

有stati修饰:类变量(在计算机里只有一份,会被类的全部对象共享)
某个数据只需要一份,且希望能够被共享(访问,修改),则该数据可以被定义成类变量来记住。
⭐类名.类变量

static String name;

无stati修饰:实例变量(属于每个对象的)
⭐对象.实例变量

int age;

②修饰成员方法

有stati修饰:类方法,属于类
⭐类名.类方法
无stati修饰:实例方法,属于对象
⭐对象.实例方法

2.继承(extends)

让一个类与另一个类建立起父子关系。

特点:

子类能继承父类的非私有成员(public:成员变量/方法)

3.方法重写

当子类觉得父类中的某个方法不好用,或者无法满足自己的需求时,子类可以重写一个方法名称,参数列表一样的方法,去覆盖父类的这个方法。
重写后,方法的访问,java会遵循就近原则
⭐注意事项:
①使用@Override注解
②子类重写父类方法时,访问权限必须大于或等于父类该方法的权限
③重写方法的返回值类型,必须与被重写方法的返回值类型一样,或者范围更小
④私有方法,静态方法不能被重写
⭐常见应用常见
子类重写Object类的toString()方法,以便返回对象的内容

4.final

①修饰类:最终类,不能被继承了

②修饰方法:最终方法,不能被重写了

③修饰变量:该变量只能被赋值一次

5.泛型

可以避免强制类型转换,及其可能出现的异常
本质:把具体的数据类型作为参数传给类型变量

        // <String>:list里面只能放String类型的数据
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("java1");
        list1.add("java2");
        list1.add("java3");
        for (int i = 0; i < list1.size(); i++) {
        // 遍历出来的数据交给String
            String e = list1.get(i);
            System.out.println(e);
        }

⭐泛型不支持基本数据类型,只能支持引用数据类型

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?