java类相关知识

class Outer{
    int score = 90;
    void inst(){
        Inner iner = new Inner();
        iner.display();
    }
    public class Inner{    //如果将其定义为static,则不能调用外部类中的非static变量score
        void display(){
            String name = "nihao";
            System.out.println("the score is " + score);
        }
    }
    public void print(){
      //  System.out.println("the name is "+ name);   //外部类无法找到内部类中声明的属性,内部类可以找到外部类声明的属性
    }
}

public class Main{
    public static void main(String[] args) {
        Outer out = new Outer();
        out.inst();

        Outer out1 = new Outer();
        Outer.Inner iner = out1.new Inner();
        iner.display();
    }

}

在方法中定义类

class Outer{
    int score = 90;
    void inst(final int s){
       // int tmp1 = 20;  //错误
        final int tmp = 20;
        class Inner{
            void display(){
                System.out.println("the score is " + (score + s + tmp));  //在方法中定义的类,可以访问final修饰的变量tmp s.不能访问没有final修饰变量tmp1
            }
        }
        Inner iner = new Inner();
        iner.display();
    }

    public void print(){
      //  System.out.println("the name is "+ name);   //外部类无法找到内部类中声明的属性,内部类可以找到外部类声明的属性
    }
}

public class Main{
    public static void main(String[] args) {
        Outer out = new Outer();
        out.inst(5);

    }

}

static用法

class Person{
    String name;
    int age;
    String city;     //如果将此句改为static String city,则最后的输出结果全部为Amarica
    public Person(String name,int age,String city){
        this.name = name;
        this.age = age;
        this.city = city;
    }
    void print(){
        System.out.println("name is " + name + "age is " + age + "city is "+ city);
    }
}

public class Main{
    public static void main(String[] args) {
        Person p1 = new Person("zhangsan",10,"china");
        Person p2 = new Person("lisi",15,"china");
        Person p3 = new Person("wangwu",20,"china");

        p1.print();
        p2.print();
        p3.print();
     
        //修改后
        p1.city = "Amarica";
        p1.print();
        p2.print();
        p3.print();
        /*
        name is zhangsan age is 10city is china
        name is lisi age is 15city is china
       name is wangwu age is 20city is china
       name is zhangsan age is 10city is Amarica
      name is lisi age is 15city is china
     name is wangwu age is 20city is china
        */
    }

}

静态代码块
当类被载入时,静态代码块被执行,且只会被执行一次,静态代码块经常被用来进行类属性的初始化

class Person{
    public Person(){
        System.out.println("1.public Person()");
    }
    static{
        System.out.println("2.Person class static code run");
    }
}

public class Main{
    static{
        System.out.println("3.Main static run");
    }
    public static void main(String[] args) {
        System.out.println("4.code run");
        new Person();
        new Person();
    }
}

运行结果是

3.Main static run
4.code run
2.Person class static code run
1.public Person()
1.public Person()

final关键字使用
1.final标志的类不能被继承
2.final标志的方法不能被子类覆写
3.final标志的变量(成员变量或者局部变量)即为常量,只能被赋值一次

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值