一、访问权限修饰符:public private protected 默认
1、public:共有的,当前工程下任何一个类都可以通过当前类访问共有的属性或方法;
2、private:私有的,只有当前类的内部可以使用;
3、protected:受保护的,只有当前工程下的同一个包通过当前对象访问其属性或方法,不同的包只能通过继承能访问;
4、默认:当前工程下的同一个包可以通过当前对象访问其属性或方法。
package huaxin_1013_1;
public class Student {
public String name = "张三";
protected int age;
String sex;
private String address = "衡阳";
public void study() {
System.out.println("学习");
}
protected void study1() {
System.out.println("学习1");
}
void study2() {
System.out.println("学习2");
}
private void study3() {
System.out.println("学习3");
}
}
package huaxin_1013_1;
public class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.name = "王五";
stu.age=10;
stu.sex="男";
// stu.address = ""; 私有的不能被访问
System.out.println(stu.name+" "+stu.age+" "+stu.sex);
stu.study();
stu.study1();
stu.study2();
//stu.study3(); 私有的不能被访问
}
}
package huaxin_1013_2;
import huaxin_1013_1.Student;
public class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.name = "王五";
//stu.age=10; 不同包不能被访问
//stu.sex="男"; 不同包不能被访问
// stu.address = ""; 私有的不能被访问
System.out.println(stu.name);
stu.study();
//stu.study1(); 不同包不能被访问
//stu.study2(); 不同包不能被访问
//stu.study3(); 私有的不能被访问
}
}
package huaxin_1013_3;
import huaxin_1013_1.Student;
public class Test extends Student {
public static void main(String[] args) {
Test t = new Test();
t.name = "李四";
t.age = 10; // 不同包的子类可以继承到
// t.age=10; 不同包不能被访问
// t.sex="男"; 不同包不能被访问
// t.address = ""; 私有的不能被访问
System.out.println(t.name+" "+t.age);
t.study();
t.study1(); // 不同包的子类可以继承到
// t.study2(); 不同包不能被访问
// t.study3(); 私有的不能被访问
}
}
二、this/super
this:当前类的对象,可以用来调用方法、构造方法、属性;
super:父类的对象,可以调用父类的方法、构造方法、属性。
注意:子类构造方法必然会调用父类的构造方法,通过super关键字调用;super关键字调用构造方法的使用必须出现在当前的函数第一行
package huaxin_1013_4;
public class A {
public String s = "蚂蚁";
public A(String s) {
System.out.println("调用了A的构造方法");
this.s=s;
}
public void study() {
System.out.println("调用了学习方法");
}
}
<pre class="java" name="code">package huaxin_1013_4;
public class B extends A {
public String w;
public B(String s) {
super(s); //调用A的构造方法
super.study(); //调用A的普通方法
System.out.println("调用了B类的构造方法");
} //B重写A的构造方法
}
package huaxin_1013_4;
public class Test {
public static void main(String[] args) {
B b =new B(" ");
}
}
执行结果:
<img src="https://img-blog.csdn.net/20161014155727547?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
三、final(最终的)
修饰对象:类、属性、方法、参数、局部变量
类:当前类不能被继承
属性:当前属性只能被赋值一次
方法:代表当前方法不能被重写
参数、局部变量:表示当前局部变量在使用范围内不能被修改
<pre class="java" name="code">package huaxin_1013_5;
public final class A {
public final int w = 10;
// public A(int w){
// this.w=w;
// }
public final void study(final int a) {
System.out.println("A在学习");
final String t;
}
}
package huaxin_1013_5;
// public class B extends A{
// public void study(){}//final修饰的方法不能被重写
// } final 修饰的类不能被继承
package huaxin_1013_5;
public class Test {
public static void main(String[] args) {
A a = new A();
// a.w=20; final修饰的属性只能被赋值一次
a.study(0);
}
}
四、static(静态的)
修饰对象:属性、方法、静态块
静态的属性和方法:在加载当前类的时候就会加载静态属性和方法到静态空间存储
注意:静态的方法不能直接使用非静态属性
package huaxin_1013_6;
public class Student {
public String address;
public static String name;// 静态属性
static {
System.out.print(1);
} // 静态块(只有在类加载时执行一次)
{
System.out.print(2);
}
public Student() {
System.out.print(3);
} //构造方法
public static void study() {
System.out.print(4);
} // 静态方法
public void play() {
System.out.print(5);
}
}
package huaxin_1013_6;
public class Test {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student();
stu1.play();
Student.study();
}
}
执行结果:
<img src="https://img-blog.csdn.net/20161014153245234?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
五、break\return\continue
break:跳出当层循环;
return:结束当前循环;
continue:跳出当次循环,直接进入下次循环。