1 访问控制修饰符(二)
1)public:公共的,可以用来修饰类,属性,构造方法以及方法,被public修饰的类,属性,构造方法以及方法,可以任意的进行访问。
2)private:私有的,可以用来修饰属性,构造方法以及方法,被private修饰的属性,构造方法以及方法,只能在本类的内部访问,外界无法访问。
3)一般针对private修饰的私有属性,都建议编写使用public修饰的get()/set()方法进行访问。
get()方法:获得属性的值。
set()方法:设置属性的值。
案例:Demo1
1public class Demo1 { 2 public static void main(String[] args) { 3 Foo1 foo = new Foo1("韩信",36); 4 int age = foo.getAge(); 5 System.out.println(age); 6 String name = foo.getName(); 7 System.out.println(name); 8 foo.setName("刘邦"); 9 String name2 = foo.getName(); 10 System.out.println(name2); 11 foo.setAge(46); 12 int age2 = foo.getAge(); 13 System.out.println(age2); 14 } 15} 16class Foo1{ 17//属性 18 private String name; 19 private int age; 20//构造方法 21 public Foo1(String name,int age){ 22 this.name = name; 23 this.age = age; 24 } 25//方法 26 public int getAge(){ 27 return age; 28 } 29 public void setAge(int age){ 30 this.age = age; 31 } 32 public String getName(){ 33 return name; 34 } 35 public void setName(String name){ 36 this.name = name; 37 } 38} 39 40public class Demo2 { 41 42} 43 44class User { 45 // 属性(private):姓名,年龄,职位,工资,地址, 46 // 邮箱,婚否(boolean marry) 47 private String name; 48 private int age; 49 private String job; 50 private double salary; 51 private String address; 52 private String email; 53 private boolean marry; 54 55 // 构造方法:给每一个属性赋值 56 public User(String name, int age, String job, double salary, 57 String address, String email, boolean marry) { 58 this.name = name; 59 this.age = age; 60 this.job = job; 61 this.salary = salary; 62 this.address = address; 63 this.email = email; 64 this.marry = marry; 65 } 66 67 // 方法:给每一个私有属性提供对应的get()/set()方法 68 public String getName() { 69 return name; 70 } 71 72 public void setName(String name) { 73 this.name = name; 74 } 75 76 public int getAge() { 77 return age; 78 } 79 80 public void setAge(int age) { 81 this.age = age; 82 } 83 84 public String getJob() { 85 return job; 86 } 87 88 public void setJob(String job) { 89 this.job = job; 90 } 91 92 public double getSalary() { 93 return salary; 94 } 95 96 public void setSalary(double salary) { 97 this.salary = salary; 98 } 99 100 public String getAddress() { 101 return address; 102 } 103 104 public void setAddress(String address) { 105 this.address = address; 106 } 107 108 public String getEmail() { 109 return email; 110 } 111 112 public void setEmail(String email) { 113 this.email = email; 114 } 115//boolean类型私有属性,get方法是以is开头 116 public boolean isMarry() { 117 return marry; 118 } 119 120 public void setMarry(boolean marry) { 121 this.marry = marry; 122 } 123 124}
4)默认的:系统默认提供访问控制修饰符,可以用来修饰类,属性,构造方法以及方法,被默认修饰类,属性,构造方法以及方法,在本包内任意访问,不同包之间无法访问。
5)不同包之间创建对象,必须要先导入该对象所属的类
import 包名.类名;
案例:Demo3
1import com.tarena.demo2.Person; 2//import 包名.类名; 3public class Demo3 { 4 public static void main(String[] args) { 5 Person p1 = new Person(); 6 System.out.println(p1.name); 7//被public修饰的属性,外界可以任意的访问。 8// System.out.println(p1.age); 9//被private修饰的属性,只能在本类的内部访问,外界 10//无法访问。 11// System.out.println(p1.address); 12//被默认修饰的属性,只能在本包内访问,不同包之间无法 13//访问。 14 } 15}
2 抽象类
含有抽象方法类,称为抽象类。
1)抽象方法:只有方法的声明(定义),而没有方法的实现的方法,称为抽象方法。
格式:
abstract 返回值类型 方法名(参数列表);
2)抽象类的组成:属性,构造方法,抽象方法和非抽象方法
格式:
abstract class 类名{
属性
构造方法
非抽象方法
抽象方法
}
eg:
abstract class Car{
String name;
String no;
int speed = 0;
Car(String name,String no){
this.name = name;
this.no = no;
}
void show(){
System.out.println(name+","+no+","+speed);
}
abstract void start();
abstract void run();
abstract void stop();
}
案例:Demo4
1public class Demo4 { 2 public static void main(String[] args) { 3// Car car = new Car(); 4//不能使用抽象类直接的创建对象。 5 Benz benz = 6 new Benz("宝马","123456","白色", 7 "豪华版",200000.1); 8 benz.show(); 9 benz.start(); 10 benz.run(); 11 benz.stop(); 12 } 13} 14abstract class Car{ 15//属性 16 String name; 17 String no; 18 String color; 19 int speed = 0; 20 String type; 21//构造方法 22 Car(String name,String no,String color, 23 String type){ 24 this.name = name; 25 this.no = no; 26 this.color = color; 27 this.type = type; 28 } 29 Car(){ 30 31 } 32//非抽象方法 33 void show(){ 34 System.out.println(name+","+no+","+ 35 color+","+type+","+speed); 36 } 37//抽象方法 38 abstract void start(); 39 abstract void run(); 40 abstract void stop(); 41} 42class Benz extends Car{ 43//属性 44 double price; 45//构造方法 46 Benz(String name,String no, 47 String color,String type, 48 double price){ 49 this.name = name; 50 this.no = no; 51 this.color = color; 52 this.type = type; 53 this.price = price; 54 } 55//方法 56 @Override 57 void run() { 58 System.out.println(name+"在行驶"); 59 } 60 @Override 61 void start() { 62 System.out.println(name+"启动了"); 63 } 64 @Override 65 void stop() { 66 System.out.println(name+"刹车了"); 67 } 68} 69 70public class Demo5 { 71 public static void main(String[] args) { 72 Midea midea = 73 new Midea("美的","壁挂式", 74 5000.33,29,"红色"); 75 midea.show(); 76 midea.hot(3); 77 midea.cool(5); 78 } 79} 80abstract class Kongtiao{ 81//属性 82 String name; 83 String type; 84 double price; 85 int degree; 86//构造方法 87 Kongtiao(String name,String type, 88 double price,int degree){ 89 this.name = name; 90 this.type = type; 91 this.price = price; 92 this.degree = degree; 93 } 94 Kongtiao(){ 95 } 96//方法 97 void show(){ 98 System.out.println(name+","+type+"," 99 +degree+","+price); 100 } 101//抽象方法 102 abstract void cool(int degree); 103 abstract void hot(int degree); 104} 105//设计一个抽象类Kongtiao 106//属性:名称,类型,价格,温度 107//构造方法:1)给每一个属性赋值;2)空参构造方法 108//方法:void show():输出每一个属性值 109//抽象方法: 110//abstract void cool(int degree): 111//降低degree度 112//abstract void hot(int degree): 113//升高degree度 114class Midea extends Kongtiao{ 115//属性:名称,类型,价格,温度,颜色 116 String color; 117//构造方法 118 Midea(String name,String type, 119 double price,int degree, 120 String color){ 121 super(name,type,price,degree); 122 this.color = color; 123 } 124//方法 125 @Override 126 void cool(int degree) { 127 this.degree = this.degree - degree; 128 System.out.println(name+"降低"+degree 129 +"度以后,当前的温度是"+this.degree); 130 } 131 @Override 132 void hot(int degree) { 133 this.degree = this.degree + degree; 134 System.out.println(name+"升高"+degree 135 +"度以后,当前的温度是"+this.degree); 136 } 137}
3)不能直接使用抽象类来创建对象,因为抽象类中含有抽象方法(功能没有实现)。
4)编写该抽象类的子类,该子类必须要重写抽象类中所有的抽象方法,实现功能,可以创建该抽象类的子类的对象。
5)抽象类是一个用来设计的工具。
企业开发中,把开发人员分成两类,一类是设计人员(项目经理),另一类是编码人员(程序员),由设计人员来设计抽象类或者接口,主要设计抽象方法,交给编码人员来编写该抽象类的子类,重写抽象方法,实现功能。
6)抽象类侧重是抽象方法的设计,尽量少非抽象方法。
3 final
最终的,最后的,可以用来修饰类,属性,方法
1)使用final修饰的类,称为最终类。该类不能再被继承。
案例:Demo6
1public class Demo6 { 2 3} 4final class Zoo1{ 5 6} 7/* 8被final修饰的类,不能再被继承(不能再有子类)。 9class Zoo2 extends Zoo1{ 10Math 11} 12*/
sun公司设计的工具类,经常使用final进行修饰,比如String,
Scanner,Math...但是在实际企业中,很少使用final修饰类。
2)使用final修饰的方法,称为最终方法。该方法不能再被方法重写。
案例:Demo7
1public class Demo7 { 2 3} 4class Moo1{ 5 final void f1(){ 6 System.out.println("努力学习,2018快到了"); 7 System.out.println("父类Moo1中编写" + 8 "方法f1()"); 9 } 10} 11class Moo2 extends Moo1{ 12/* 13被final修饰的方法,不能再被子类方法重写。 14 void f1(){ 15 System.out.println("子类Moo2中重写" + 16 "父类Moo1中的方法f1()"); 17 } 18*/ 19}
3)使用final修饰的属性,该属性的值一旦初始化以后,其属性值不会再被修改。
案例:Demo8
1public class Demo8 { 2 public static void main(String[] args) { 3 Yoo1 yoo = new Yoo1(); 4// yoo.num = 200; 5//被final修饰的属性,其属性一旦初始化以后,其属性值 6//不能再被修改。 7 } 8} 9class Yoo1{ 10 final int num = 100; 11 void updateNum(){ 12// num = 200; 13 } 14}
4 常量
java中的常量外界可以任意的访问(public),常量不依赖于对象而存在,在内存中单独开辟存储空间(static),常量的值永远都不能被修改(final)。
1) 定义常量格式:
public static final 数据类型 常量名 = 数值
或者
public final static 数据类型 常量名 = 数值
2) 常量的名字中所有字母建议都大写。
eg:
public static final double MAX_PRICE = 10000000.99;
public final static double MIN_SALARY = 10000000.99;
public static final String LOGIN_NAME = "admin";
案例:Demo9
1public class Demo9 { 2 public static void main(String[] args){ 3//类名.常量名 4 System.out.println(Eoo.MIN_PRICE); 5 System.out.println(Eoo.MAZ_SALARY); 6 System.out.println(Eoo.LOGIN_NAME); 7//对象名.常量名 8 Eoo eoo = new Eoo(); 9 System.out.println(eoo.MIN_PRICE); 10 System.out.println(eoo.MAZ_SALARY); 11 System.out.println(eoo.LOGIN_NAME); 12 } 13} 14class Eoo{ 15 public static final 16 double MIN_PRICE = 2000000.1; 17 public final static 18 double MAZ_SALARY = 2000000.99; 19 public static final 20 String LOGIN_NAME = "admin"; 21}
3) 访问常量:
类名.常量名
或者
对象名.常量名
5 父类声明指向子类对象(了解)
使用父类进行声明(定义)对象,使用子类的构造方法来创建对象。
1)格式:
父类 对象名 = new 子类构造方法
2)使用父类声明指向子类对象这种方式所创建的对象,可以使用从父类中继承过来的方法。
案例:Demo10
1public class Demo10 { 2//父类声明指向子类对象:使用父类来声明(定义)对象, 3//调用子类的构造方法来创建对象。 4//父类 对象名 = new 子类构造方法 5 public static void main(String[] args) { 6 Noo1 noo = new Noo2(); 7 noo.f1(); 8//使用这种方式所创建的对象,可以访问从父类中继承 9//过来的方法。 10 } 11} 12class Noo1{//父类 13 void f1(){ 14 System.out.println("努力学习"); 15 System.out.println("明天周五"); 16 System.out.println("在父类Noo1中编写" + 17 "方法f1()"); 18 } 19} 20class Noo2 extends Noo1{//子类 21 22}
3)使用父类声明指向子类对象这种方式所创建的对象,可以使用子类重写父类的方法。
案例:Demo11
1public class Demo11 { 2 public static void main(String[] args) { 3//父类 对象名 = new 子类构造方法 4 Koo1 koo = new Koo2(); 5 koo.f1(); 6//使用父类声明指向子类对象这种所创建的对象,可以访问 7//子类重写父类的方法。 8 } 9} 10class Koo1{ 11 void f1(){ 12 System.out.println("父类Koo1中编写的" + 13 "方法f1()"); 14 } 15} 16class Koo2 extends Koo1{ 17 void f1(){ 18 System.out.println("子类Koo2重写父类" + 19 "Koo1中的方法f1()"); 20 } 21}
4)使用父类声明指向子类对象这种方法所创建的对象,不可以访问子类中单独编写的方法(非继承或者重写)
案例:Demo12
1public class Demo12 { 2 public static void main(String[] args) { 3 Coo2 coo = new Coo2(); 4 coo.f1(); 5 coo.f2(); 6//父类 对象 = new 子类构造方法 7 Coo1 coo2 = new Coo2(); 8 coo2.f1(); 9// coo2.f2(); 10//使用父类声明指向子类对象这种方式所创建的对象,可以 11//访问父类中声明(定义)过的方法(继承或者重写)。不能 12//访问子类中单独编写的方法(非继承或者重写) 13 } 14} 15class Coo1{ 16 void f1(){ 17 System.out.println("父类Coo1中编写" + 18 "的方法f1()"); 19 } 20} 21class Coo2 extends Coo1{ 22 void f1(){ 23 System.out.println("子类Coo2重写父类" + 24 "Coo1中的方法f1()"); 25 } 26 void f2(){ 27 System.out.println("子类Coo2中单独" + 28 "编写的方法"); 29 } 30}
总结:使用父类声明指向子类对象这种方式所创建的对象,可以访问在父类中声明(定义)过方法,不能访问在子类中单独编写的方法。