本章概述
本章属于面向对象第二章的内容,主要讲解this关键字、static关键字、代码块、package、import、面向对象三大特征之一---封装等知识点。
一、this关键字
- this的作用:
- this表示的是当前对象本身,
- 更准确地说,this代表当前对象的一个引用。
2.普通方法中使用this
- 区分类成员属性和方法的形参
- 调用当前对象的其他方法(可以省略)
- 位置:任意
3.构造方法中使用this
- 使用this来调用其它构造方法
- 位置:必须是第一条语句
4.this不能用于static方法。(讲完static,大家就知道为什么了!)
5.this测试代码
二、static关键字
1.在类中,用static声明的成员变量为静态成员变量 ,或者叫做: 类属性,类变量.
- 它为该类的公用变量,属于类,被该类的所有实例共享,在类被载入时被显式初始化
- 对于该类的所有对象来说,static成员变量只有一份。被该类的所有对象共享
- 可以使用”对象.类属性”来调用。不过,一般都是用“类名.类属性”
- static变量置于方法区中
2.用static声明的方法为静态方法
- 不需要对象,就可以调用(类名.方法名)
- 在调用该方法时,不会将对象的引用传递给它,所以在static方法中不可访问非static的成员
- 静态方法不能以任何方式引用this和super关键字
3.static示例代码
public class TestStatic {
int a;
static int width;
static void gg(){
System.out.println("gg");
}
void tt(){
System.out.println("tt");
}
public static void main(String[] args){
TestStatic hi = new TestStatic();
TestStatic.width = 2;
TestStatic.gg(); //gg();
//通过引用也可以访问static变量或static方法。不过,一般还是使用类名.static成员名来访问。
hi.gg();
gg();
}
}
4.static关键字
- 使用static声明的成员变量称为静态变量,
- 使用static声明的方法称为静态方法
- 静态变量和静态方法又称为类变量和类方法
- static关键字用法-课堂demo【重点掌握】
package netclass02;
/**
* @Auther: Yu Panpan
* @Date: 2022/1/7 - 01 - 07 - 15:17
* @Description: netclass02
* @version: 1.0
*/
/*
* 使用static统计在类中一共产生多少个对象?
*/
public class StaticDemo2 {
static int count;
// int count;
public StaticDemo2(){
count++;
System.out.println("创建了" +count + "个对象");
}
public static void main(String[] args) {
new StaticDemo2();//1
new StaticDemo2();//2
new StaticDemo2();//3
}
}
5.静态属性的访问形式
- 对象名.属性
- 类名.属性
6.静态方法
- 访问修饰符 static 返回值类型 方法名(){}
7.访问形式
- 对象名.方法名()
- 类名.方法名()
8.常见错误1
public void showInfo(){
System.out.println("姓名:"+this.name+"\t年龄:"+this.age+"\t城市:"+this.country);
}
public static void welcome(){
this.showInfo();//调用本类的非静态方法
System.out.println("欢迎大家来腾迅互联学习......");
}
9.常见错误2
请指出下面代码错误
10.小结
static修饰与非static修饰的区别
static、非private修饰 | 非static、private修饰 | |
---|---|---|
属性 | 类属性、类变量 | 实例属性、实例变量 |
方法 | 类方法 | 实例方法 |
调用方式 | 类名.属性 类名.方法() 对象.属性 对象.方法() | 对象.属性 对象.方法() |
归属 | 类 | 单个对象 |
11.课堂demo【重点掌握】 |
package netclass02;
/**
* @Auther: Yu Panpan
* @Date: 2022/1/7 - 01 - 07 - 14:46
* @Description: netclass02
* @version: 1.0
*/
/*
* static:
* 修饰成员变量的时候,表示静态成员变量或者叫类变量
* 普通变量在使用的时候,必须要通过对象名进行调用
* 类变量或者静态变量可以使用对象名调用也可以使用类名进行调用
* 修饰方法的时候,表示静态方法或者叫类方法
* 普通方法在使用的时候,必须要通过对象名进行调用
* 类方法或者静态方法可以使用类名,也可以使用对象名
* 注意:
* 1、静态变量,在创建对象之前被初始化,或者说在类被载入之前进行初始化
* 2、静态变量被所有的对象共享,属于公共变量,对象和类都可以直接调用,但是推荐使用类来调用
* 3、成员变量放在堆中,而静态变量放在方法区中的静态区中
* 4、静态变量不能定义在静态方法中
* 5、静态方法可以在非静态方法中进行调用
* 6、静态方法中不能直接调用非静态方法,换句话说可以间接调用非静态方法
* 7、静态方法中不允许出现this调用
* 8、一般工具类中的方法定义为static
*/
public class StaticDemo {
//成员变量
String name = "zhangfei";
static int age = 20; //静态成员变量
// public StaticDemo(String name,int age){
// this.name = name;
// this.age = age;
// }
public void test1(){
System.out.println("test1 is a non-static method");
// static int a = 10;
// test2();
}
public static void test2(){
// this.test1();
System.out.println("test2 is a static method");
// static int a = 10;
// test1();
new StaticDemo().test1(); //间接调用
}
public static void main(String[] args) {
StaticDemo sd = new StaticDemo();
System.out.println(sd.name);
System.out.println(sd.age);
// sd.age = 30;
// System.out.println(sd.age);//30
// System.out.println(sd.age);//30
//
// sd.age = 40;
// System.out.println(sd.age);//40
// System.out.println(StaticDemo.age);//40
StaticDemo staticDemo = new StaticDemo();
staticDemo.test1();
// StaticDemo.test2();
// staticDemo.test2();
// StaticDemo sd2 =new StaticDemo();
// System.out.println(sd2.name);
}
}
更多Java基础知识 点击下载 Java学习手册.pdf文档
或加入 Java学习交流群组有问题随时问