Java开发必备的三大修饰符

网友投稿 212 2023-01-10

Java开发必备的三大修饰符

一、abstract 抽象的

抽象类:被abstract 修饰的类

语法: abstract class 类名{}

抽象方法 : 被abstract 修饰的方法

定义语法:

访问修饰符 abstract 返回值类型 方法名(参数列表);

abstract 访问修饰符 返回值类型 方法名(参数列表);

特点:

1.当一个类中存在抽象方法 那么这个类也必须是抽象的

2.一个抽象类中可以有抽象方法也可以有非抽象方法

3.如果一个类继承了抽象类,这个类如果不想成为抽象类,那么这个类必须实现抽象类中的所有抽象方法

4.抽象类不能创建对象,但是可以声明引用,抽象类的对象是存在的,是由子类创建子类对象时调用父类构造方法创建的,是无法自己手动去new 抽象类的对象的

抽象类的好处:强制使用多态

案例:

public class Demo{

public static void main(String[] args) {

}

}

abstract class Animal{

//抽象方法

public abstract void eat();

public abstract void sleep();

public void m1() {

System.out.println("m1");

}

}

class Dog extends Animal{

public void eat(){

System.out.println("狗吃屎");

}

public void sleep(){

System.out.println("狗睡");

}

}

案例:

public class Demo{

public static void main(String[] args) {

Animal a = new Dog();

}

}

abstract class Animal{

public Animal() {

System.out.println("动物类的构造被调用 创建了 对象");

}

//抽象方法

abstract public void eat();

public abstract void sleep();

public void m1() {

System.out.println("m1");

}

}

class Dog extends Animal{

public void eat(){

System.out.println("狗吃屎");

}

public void sleep(){

System.out.println("狗睡");

}

}

二、static 静态的

static修饰成员变量:类变量 静态变量 静态属性

定义语法:

访问修饰符 static 数据类型 变量名 = 变量值;

static 访问修饰符 数据类型 变量名 = 变量值;

访问的方式(特点)

1.类变量可以用 类名.属性名 访问

2.可以通过创建对象 用引用去访问 (不推荐)

案例:

public class Demo{

public static void main(String[] args) {

Test t1 = new Test();

Test t2 = new Test();

t1.a++;

t1.b++;

System.out.println(t2.a);//10

System.out.println(t2.b);//21

}

}

class Test{

int a = 10;//实例变量

static int b = 20;//类变量

}

案例2:

public class Demo{

public static void main(String[] args) {

/*Test t1 = new Test();

Test t2 = new Test();

t1.a++;

t1.b++;

System.out.println(t2.a);

System.out.println(t2.b);*/

System.out.println(Test.b);//20

}

}

class Test{

int a = 10;//实例变量

static int b = 20;//类变量

}

static修饰成员方法: 静态方法

语法:

访问修饰符 static 返回值类型 方法名(形参列表){

方法的实现;

static 访问修饰符 返回值类型 方法名(形参列表){

方法的实现;

特点:

1.静态的方法中 不可以直接访问非静态的成员(成员变量 和 成员方法)

2.如果要访问非静态的成员 必须创建对象 通过引用取访问

3.静态方法可以通过 类名.方法名() 访问 也可以通过引用去访问(不建议)

4.静态的方法可以被继承 静态的方法不能被非静态的方法所覆盖 只能被静态方法覆盖 但是没有多态(引用是什么类型 调用的方法就是这个类型中的方法)

5.在静态方法中是不可以使用 this 和 super 关键字 因为 this 和 super都是和对象有关的 而静态的成员和对象无关 先于对象存在

案例:关于静态方法 访问非静态的成员

public class Demo{

public static void main(String[] args) {

/*Test t1 = new Test();

Test t2 = new Test();

t1.a++;

t1.b++;

System.out.println(t2.a);

System.out.println(t2.b);*/

//System.out.println(Test.b);//20

}

}

class Test{

int a = 10;//实例变量

static int b = 20;//类变量

//静态方法

public static void m1() {

m2();

//System.out.println(a);

}

//成员方法

public void m2() {

}

}

案例:关于 静态方法的使用 和 通过引用去访问非静态的成员

public class Demo{

public static void main(String[] args) {

//Test.m1();

Test t = new Test();

t.m1();

}

}

class Test{

int a = 10;//实例变量

static int b = 20;//类变量

//静态方法

public static void m1() {

Test t = new Test();

System.out.println(t.a);

t.m2();

}

//成员方法

public void m2() {

System.out.println("m2");

}

}

案例:关于 静态方法被继承

public class Demo{

public static void main(String[] args) {

/*Dog d = new Dog();

d.eat();*/

Dog.eat();

}

}

//定义动物类

class Animal{

public static void eat() {

System.out.println("动物吃");

}

}

class Dog extends Animal{

}

案例:关于 静态方法中是否能使用 this 和 super

public class Demo{

public static void main(String[] args) {

/*Dog d = new Dog();

d.eat();*/

//Dog.eat();

/*Animal a = new Dog();

a.eat();*/

Dog.eat();

}

}

//定义动物类

class Animal{

String sex = "狗妖";

public static void eat() {

System.out.println("动物吃");

}

}

class Dog extends Animal{

/*static String name = "金毛";

static int age = 3;*/

String sex = "母";//成员变量

public static void eat() {

String sex = "公"; //局部变量

System.out.println(super.sex);

/*System.out.println(name);

System.out.println(age);

System.out.println("狗吃");*/

}

}

初始代码块:定义在类中 方法外 用于在创建对象时 和 成员变量 按照从上向下的顺序执行初始化的操作 也叫做 动态代码块

语法:

{初始化代码块 }

案例:

public class Demo{

public static void main(String[] args) {

Test t = new Test();

System.out.println(t.a);

System.out.println(t.b);

System.out.println(t.c);

}

}

class Test{

int a = 10;

int c;

int b;

{//初始化属性的

c = 30;

b = 20;

}

}

static 修饰初始化代码块:静态代码块

静态代码块的作用:在类加载时 和静态的属性 按照顺序执行 为类进行初始化操作

语法:

static{

}

案例:

public class Demo{

public static void main(String[] args) {

System.out.println(Test.a);

System.out.println(Test.b);

}

}

class Test{

static int a = 20;

static{

b = 40;

}

static int b;

}

注意:静态变量 时有默认值 先进行赋默认值 再初始化

==================================================================

类加载:当jvm第一次使用一个类时 需要通过classpath 找到.class = 字节码文件

读入这个类中的信息(包名 类名 属性 方法 静态的变量 静态的方法 。。。)并保存在虚拟机中 类加载只进行一次

类加载的时机:

1.创建对象时

2.类名访问静态成员(静态属性 静态方法)

3.子类进行类加载时 会先进行父类的类加载

案例:关于 1和2 两种情况类加载

public class Demo{

public static void main(String[] args) {

//1.创建对象

//Test t = new Test();

//2. 访问静态的成员

System.out.println(Test.a);

}

}

class Test{

stCLNYHqbTmNatic int a = 20;

static{

System.out.println("父类 类加载了");

}

}

案例:关于 3 情况的类加载

public class Demo{

public static void main(String[] args) {

//创建子类的对象

//Sub s = new Sub();

System.out.println(Sub.b);

}

}

class Test{

static int ahttp:// = 20;

static{

System.out.println("父类 类加载了");

}

}

class Sub extends Test{

static int b = 30;

static{

System.out.println("子类进行类加载了");

}

}

三、final 最终的

修饰变量

局部变量:final修饰的局部变量只能赋值一次 值一旦赋值不可以改变

常量的定义:

public static final double PI = 3.14159265457;

案例:

public class Demo{

public static void main(String[] args) {

int a = 20;//局部变量

final int b = 10;

a++;

b++;

System.out.println(a);

System.out.println(b);

}

}

public class Demo{

public static void main(String[] args) {

final int a ;//声明

a = 10;

a = 20;//报错

System.out.println(a);

}

}

实例变量

特点:

1.final修饰的实例变量没有默认值

2.final修饰的实例变量只能赋值一次

3.final修饰的实例变量可以在构造方法中初始化 但是要保证每个构造都必须能够为这个变量初始化

案例:

public class Demo{

public static void main(String[] args) {

Animal a = new Animal(250);

//System.out.println(a.a);//0

System.out.println(a.b);

}

}

class Animal{

/*int a = 10;//实例变量

final int b = 20;*/

int a;

final int b = 20;

public Animal(int b) {

this.b = b;

}

}

public class Demo{

public static void main(String[] args) {

Animal a = new Animal(250);

//System.out.println(a.a);//0

System.out.println(a.b);

}

}

class Animal{

/*int a = 10;//实例变量

final int b = 20;*/

int a;

final int b ;

public Animal(int b) {

this.b = b;

}

public Animal() {

b = 30;

}

}

类变量

特点:

1.final修饰的类变量没有默认值

2.final修饰的类变量可以通过静态代码块进行初始化

案例:

public class Demo{

public static void main(String[] args) {

System.out.println(Animal.a);

System.out.println(Animal.b);

}

}

class Animal{

static int a ;

final static int b ;

static{

b = 250;

}

}

修饰方法

成员方法:

特点:

1.final修饰的成员方法可以重载

2.final修饰的成员方法不可以被覆盖

语法:

public final void m1() {

}

final public void m1() {

}

案例:

public class Demo{

public static void main(String[] args) {

}

}

class Animal{

public void m1() {

System.out.println("m1");

}

}

class Dog extends Animal{

public final void m1() {

System.out.println();

}

}

静态方法:

特点:

1.final修饰的静态方法可以重载

2.final修饰的静态方法不可以被覆盖

案例:

public class Demo{

public static void main(String[] args) {

}

}

class Animal{

public final static void m1() {

System.out.println("m1");

}

public final statihttp://c void m1(int a) {

System.out.println("m1");

}

}

class Dog extends Animal{

}

修饰类

语法:

final class Animal{

}

特点:final修饰的类没有子类 俗称断子绝孙类

jdk的类库中有哪些final修饰的类?

String Math System

案例:

public class Demo{

http:// public static void main(String[] args) {

}

}

class Dog extends System{

}

四、关于修饰符混搭的原则

class Animal{

public final static void m1() {

System.out.println("m1");

}

public final static void m1(int a) {

System.out.println("m1");

}

}

class Dog extends Animal{

}

##### 修饰类

```java

语法:

final class Animal{

}

特点:final修饰的类没有子类 俗称断子绝孙类

jdk的类库中有哪些final修饰的类?

String Math System

案例:

public class Demo{

public static void main(String[] args) {

}

}

class Dog extends System{

}

五、关于修饰符混搭的原则

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:智能开关免费api(高端智能开关)
下一篇:Java设计模式常用的七大原则总结
相关文章

 发表评论

暂时没有评论,来抢沙发吧~