Java——08——关键字的使用

it2026-08-09  1

文章目录

一:package二:import关键字三:super1.super调用属性和方法2.super调用构造器 四:instanceof五:static12.类变量和实例变量内存解析3.static修饰方法4.开发中如何确定一个属性是否需要声明static5.开发中如何确定一个方法是否需要声明static6.static关键字的应用1.2. 六:final1.final面试题2.3.五个小问题

一:package

二:import关键字

自定义类要导包 不然会报错

package com.xx.study; import java.util.HashMap; public class Person { public static void main(String[] args) { HashMap map =new HashMap(); } }

三:super

1.super调用属性和方法

当属性重名是,super调用的是父类的,this先找子类的 Person.java

package com.xx.study; public class Person { String name; int age; int id=1001;//身份证号 public Person() {} public Person(String name,int age) { this.name=name; this.age=age; } public void eat() { System.out.println("吃饭"); } public void sleep(int time) { System.out.println("睡觉"+time+"小时"); } }

Student.java

package com.xx.study; public class Student extends Person { String major; //和父类都有id,内存中两个id,属性和方法不一样,方法会覆盖,属性不会 int id=1002;//学号 public Student() { } public Student(String major) { this.major = major; } public void study() { System.out.println("学习java"); } // 对父类的eat方法重写 public void eat() { System.out.println("吃减脂餐"); } public void show() { //System.out.println(name+"年龄是"+age); //可以用this调用也可以用super, System.out.println(this.name+"年龄是"+super.age); System.out.println(id); System.out.println(this.id);//调用的是子类的id System.out.println(super.id);//调用的是父类的id } }

SuperTest.java

package com.xx.study; public class SuperTest { public static void main(String[] args) { Student s=new Student(); s.show(); } }

2.super调用构造器

4.4 在构造器的首行,没有显示的声明“this(形参列表)”或super(形参列表)”,则默认调用的是父类中空参的构造器,默认有一个super()

Person.java

package com.xx.study; public class Person { String name; int age; int id=1001;//身份证号 public Person() { System.out.println("测试在子类构造器中不声明this,或super,默认调用super()"); } public Person(String name,int age) { this.name=name; this.age=age; } public void eat() { System.out.println("吃饭"); } public void sleep(int time) { System.out.println("睡觉"+time+"小时"); } }

Student.java

package com.xx.study; public class Student extends Person { String major; //和父类都有id,内存中两个id,属性和方法不一样,方法会覆盖,属性不会 int id=1002;//学号 public Student() { } public Student(String major) { //super();默认有 this.major = major; } public Student(String name,int age,String major) { //this.name=name; //this.age=age; super(name,age);//调用父类的构造器 ctrl+鼠标左键 this.major=major; } public void study() { System.out.println("学习java"); } // 对父类的eat方法重写 public void eat() { System.out.println("吃减脂餐"); } public void show() { //System.out.println(name+"年龄是"+age); //可以用this调用也可以用super, System.out.println(this.name+"年龄是"+super.age); System.out.println(id); System.out.println(this.id);//调用的是子类的id System.out.println(super.id);//调用的是父类的id this.eat(); super.eat(); } }

SuperTest.java

package com.xx.study; public class SuperTest { public static void main(String[] args) { Student s=new Student(); s.show(); Student s1=new Student("Jack马",55,"阿里"); s1.show(); System.out.println("********************"); Student s2=new Student(); s2.show(); } }

四:instanceof

五:static

1

package com.xx.study1030; public class StaticTest { public static void main(String[] args) { Chinese c1 = new Chinese(); c1.name = "马化腾"; c1.age = 20; Chinese c2 = new Chinese(); c2.name = "马云"; c2.age = 50; c1.nation = "中国"; System.out.println(c2.nation); } } class Chinese { String name; int age; static String nation; }

2.类变量和实例变量内存解析

3.static修饰方法

原因在于其生命周期:先是类的加载,静态结构也跟着加载到内存当中,创建对象后对象出生,非静态结构加载,对象消亡,非静态结构就不能调用了,最后类消亡,此时静态结构也消亡 结论:静态结构跟着类加载消亡,非静态结构是跟着对象加载消亡,因此早出生的不能调晚出生的,晚出生的可以调早出生的

package com.xx.study1030; public class StaticTest { public static void main(String[] args) { Chinese c1 = new Chinese(); c1.name = "马化腾"; c1.age = 20; Chinese c2 = new Chinese(); c2.name = "马云"; c2.age = 50; c1.nation = "中国"; System.out.println(c2.nation); c1.eat(); //报错 Chinese.eat(); //静态方法可以用类调用 Chinese.show(); } } class Chinese { String name; int age; static String nation; public void eat() { System.out.println("吃米饭"); //调用非静态结构 //谁调用eat谁就是this,就输出谁的name,这个谁指的对象 this.info(); //调用静态结构 walk(); //nation前面省略的是类Chinese Chinese.nation System.out.println("nation:"+nation); } //随着类的加载而加载 public static void show() { //只能调用静态方法或属性 eat(); System.out.println("我是静态方法"); //类调用,不是this调用 System.out.println(Chinese.nation); //省略 System.out.println(nation); walk(); } public static void walk() { System.out.println("走路"); } public void info() { System.out.println(name); } }

4.开发中如何确定一个属性是否需要声明static

属性是可以被多个对象所共享的,不会随着对象的不同而不同的 类中的常量也常常声明为static

5.开发中如何确定一个方法是否需要声明static

操作静态属性的方法,通常设置为static 工具类中的方法,习惯上声明为static,这样就不用了new对象了,直接用类调用 例如:Math,Arrays,Collection

6.static关键字的应用

1.

package com.xx.study1102; public class CircleTest { public static void main(String[] args) { Circle c1=new Circle(); Circle c2=new Circle(); Circle c3=new Circle(3.4); System.out.println("c1的id:"+c1.getId()); System.out.println("c1的id:"+c2.getId()); System.out.println("创建的圆的个数"+Circle.getTotal()); } } class Circle{ private double radius; private int id; //构造器 public Circle() { id=init++; total++; } public Circle(double radius) { /*用this()调用构造器 id=init++; totao99;*/ this(); this.radius=radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public int getId() { return id; } //属性是static 属性的方法也是static public static int getTotal() { return total; } private static int total;//记录创建圆的个数 private static int init=1001;//被所有对象共享 public double findArea() { return 3.14*radius*radius; } }

2.

package com.xx.study1102; public class Account { private int id; private String pwd; private double balance; private static double interesRate; private static double minMoney = 1.0; private static int init = 1001;// 自动生成id值 public Account() { id=init++; } public Account(String pwd,double balance) { id=init++; this.pwd=pwd; this.balance=balance; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public static double getInteresRate() { return interesRate; } public static void setInteresRate(double interesRate) { Account.interesRate = interesRate; } public static double getMinMoney() { return minMoney; } public static void setMinMoney(double minMoney) { Account.minMoney = minMoney; } public int getId() { return id; } public double getBalance() { return balance; } //Alt+shift+s选择Generate toString() /* 在Java里面所有的类都有一个共同的父类Object, 不管你愿不愿意都得继承他(默认继承,不用加extends)。 那么我们今天要说的toString方法就是存在于Object里面的一个方法, 换而言之,所有的类都会具有这个方法。 */ @Override public String toString() { return "Account [id=" + id + ", pwd=" + pwd + ", balance=" + balance + "]"; } }

六:final

package com.xx.study1102; public class FinalTest { //常量习惯写成大写 final int WIDTH=0; public void show() { final int num=10;//常量 } public void show(final int num) { //num=20; 编译不通过 在这个方法内只能调用,不能赋值 System.out.println(num); } public static void main(String[] args) { FinalTest test=new FinalTest(); test.show(10); } }

1.final面试题

return ++x是错误的,这个是先++,再赋值,return x+1是可以的。

2.

属性是可以变的,对象不能变,所以o=new Other()错了,o.i++是对的

3.五个小问题

最新回复(0)