类
- 权限修饰符
访问包位置 | private | protected | public |
---|---|---|---|
本类 | 可见 | 可见 | 可见 |
同包其他类或子类 | 不可见 | 可见 | 可见 |
其他包的类或子类 | 不可见 | 不可见 | 可见 |
当声明类时未使用public、protected和private修饰符设置类的权限,则这个类预设为包存取范围,即只有一个包中的类可以调用这个类的成员变量或成员方法。
package zzz;
class some{
public void doString(){//等同于void doString(){
...//方法体
}
}
上述代码,类的修饰符为默认修饰符,即只有一个包内的其他类和子类才可以对该类访问,尽管类中的doString()方法体被设置为public,但其访问权限仍和类some相同。
- 局部变量:在成员方法内定义的变量。
在方法被执行时创建、在方法之行结束时被销毁。
局部变量在使用时必须进行赋值操作或被初试化,否则会出现编译错误。(成员变量不需要)
public class Mytest{
String name; //成员变量没有初始化,不会报错
public void getName(){ //成员方法
int id = 0; // 局部变量必须初始化赋值,否则会报错
System.out.println(id+name);
}
public static void main(String[] args){
Mytest test = new Mytest(); //创建类
test.getName(); //调用方法
}
}
//输出结果:0null
- this关键字
this.属性名称 :指的是访问类中的成员变量,用来区分成员变量和局部变量(类中的成员变量与成员方法中的参数重名问题)
https://blog.csdn.net/weixin_42386014/article/details/81138684 - 类的构造方法
构造方法是一个与类同名的方法,对象的创建通过构造方法完成,每当类实例化一个对象时,类都会自动调用构造方法。
public class Bird{
public Bird(){} // Bird的类的构造方法
}
-
构造方法就是创建类的对象过程中的运行方法,也就是对象的初始化方法。
-
构造方法没有返回值。
-
没有明确定义构造方法,编译器会自动创建一个不带参数的默认构造方法。
-
若定义了有参构造方法,编译器不会自动创建无参的构造方法,此时不可使用无参的
-
this可以调用类的成员变量和成员方法,还可以调用类中的构造方法。
public class Mytest{
int i;
public Mytest(){ //不带参数的构造方法
this(1); //使用this调用本类有参数的构造方法
}
public Mytest(int i){ //带参数的构造方法
this.i = i; //使用this调用本类成员变量
}
}
public class EggCake {
int eggCount;
public EggCake(int eggCount) { //有参构造方法
this.eggCount = eggCount;
System.out.println("这个饼里有" + eggCount + "个鸡蛋");
}
public EggCake() { //无参构造方法
eggCount = 1;
System.out.println("这个饼里有" + eggCount + "个鸡蛋");
// 或者注释上面两行直接 this(1);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
EggCake e = new EggCake();
EggCake f = new EggCake(5);
}
}
- 私有构造方法
构造方法也可以用private修饰,私有的构造方法无法在本类外部使用,也就导致本类无法用new实例化。
public class Book{
// 私有构造方法
private Book(){
}
//静态公开方法,向图书馆借书
static public Book libraryBorrow(){
return new Book();
}
}
//新建类
public class Mytest{
public static void main(String[] args){
Book book = Book.libraryBorrow(); //这里不能使用 Book book = new Book();,没有权限
}
}
- static关键字
-
由static修饰的变量、常量、和方法被称作静态变量、静态常量、静态方法,都存放在内存的“静态区”中,其生存周期是整个程序的生存周期。
-
内存中静态区的变量可以被本类共享,其他类调用本类静态变量和静态方法时,无需实例化就可以调用。
-
静态变量
public class Pool{
static public int water = 0; //水量共享的,用static修饰,水池的静态变量
public void outlet(){ //放水的方法
if(water>=2){
water -= 2;
}else{
water = 0;
}
}
public void inlet(){ //进水的方法体
water+=3;
}
public static void main(String[] args){
Pool out = new Pool(); //创建出水的水池对象
Pool in = new Pool(); //创建进水的水池对象
System.out.println("水池中的水量"+Pool.water); //水池中静态变量
System.out.println("向水池中注水两次");
in.inlet();
in.inlet();
System.out.println("水池中的水量"+Pool.water);
System.out.println("水池放水一次");
out.outlet();
System.out.println("水池中的水量"+Pool.water);
}
}
同一个类的不同实例对象,公用同一个静态变量,如果一个对象将其更改,另一个对象的静态变量也会更改。
public class StaticVariable {
static int x; //静态变量
int y;
public StaticVariable(int x, int y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
StaticVariable a = new StaticVariable(1, 2);
StaticVariable b = new StaticVariable(13, 27);
//当b将x的值赋成了13,因为x的值是共享的,所以a的x值也变成了13
System.out.println("a.x的值= "+a.x);
System.out.println("a.y的值= "+a.y);
System.out.println("b.x的值= "+b.x);
System.out.println("b.y的值= "+b.y);
}
}
/*输出的结果
a.x的值= 13 //当b将x的值赋成了13,因为x的值是共享的,所以a的x值也变成了13
a.y的值= 2
b.x的值= 13
b.y的值= 27
*/
- 用final static修饰一个成员变量,这个成员变量就会变成一个静态常量。静态常量必须是大写的且不能被修改
将例圆周率π定义为静态常量final sttic double PI= 3.1415926;
- 静态方法
public class StaticMethod{
static public void show(){
System.out.println("静态方法无需实例化就可以调用");
}
public static void main(String[] args){
StaticMethod.show();
}
}
- 静态代码块
在类中成员方法之外,用static修饰代码区域可以称之为静态代码块。定义一块静态代码块,可以完成类的初始化操作,在类声明时就会运行。
public class StaticTest{
static String name;
static{
System.out.println(name+"静态代码块");
}
{
System.out.println(name+"非静态代码块");
}
// 不管这里写的代码先后顺序如何,静态代码块总会在非静态代码块之前运行。
public StaticTest(String a){
name = a;
System.out.println(name+"构造方法");
}
public void method(){ //成员方法只有在调用时才会运行
Syetem.oout.println(name+"成员方法");
}
public static void main(String[] args){
StaticTest test = new StaticTest(); //构造方法在**new**的时候就会运行
test.method(); //调用成员方法
}
}
- 类的主方法
- 主方法是类的 的入口点,它定义了程序从何处开始;主方法提供对程序流向的控制,java编译器通过主方法来执行程序。
- 主方法的语法
public static void main(String[] args){ //方法体 }
- 主方法也是静态的,在主方法中调用其它方法必须是静态的,不是静态的必须创建对象的实例。
- 主方法没有返回值(void)。
- 主方法的形参为数组。其中args[0]~args[n]分别代表程序的第一个参数到第n个参数,可以使用args.length获取参数的个数。
- 对象的创建
- Java中使用new操作符调用构造方法就可以创建一个对象。
创建Test类对象:
Test test = new Test(); //无参
Test test = new Test("a"); //有参
特殊例子:String str = "abc";
-
对象的引用 :
Book a = new Book();
Book是指类,a是引用(操作标识符),new Book()表示对象的实体。 -
对象的使用
当使用new操作符创建一个对象后,可以使用“对象.类成员”(类成员不是用private来修饰)来获取对象的属性和行为。对象的属性和行为在类中是通过类成员变量和成员方法的形式来表示的,,所以当对象被实例化后,也就获得了相应的属性和行为。
public class TransferProperty {
int i = 47;
public void call() {
System.out.println("调用call方法");
for (i = 0; i < 3; i++) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
TransferProperty t1 = new TransferProperty();
TransferProperty t2 = new TransferProperty();
t2.i=60;
System.out.println("第一次实例对象调用i的结果"+t1.i);
t1.call();
System.out.println("第二次实例对象调用i的结果"+t2.i);
t2.call();
}
}
- 对象的销毁
finalize()方法
System.gc()