1 访问控制修饰符(二)

  1)public:公共的,可以用来修饰类,属性,构造方法以及方法,被public修饰的类,属性,构造方法以及方法,可以任意的进行访问。
  2)private:私有的,可以用来修饰属性,构造方法以及方法,被private修饰的属性,构造方法以及方法,只能在本类的内部访问,外界无法访问。
  3)一般针对private修饰的私有属性,都建议编写使用public修饰的get()/set()方法进行访问。
    get()方法:获得属性的值。
    set()方法:设置属性的值。
    案例:Demo1

public class Demo1 {
public static void main(String[] args) {
Foo1 foo = new Foo1("韩信",36);
int age = foo.getAge();
System.out.println(age);
String name = foo.getName();
System.out.println(name);
foo.setName("刘邦");
String name2 = foo.getName();
System.out.println(name2);
foo.setAge(46);
int age2 = foo.getAge();
System.out.println(age2);
}
}
class Foo1{
//属性
private String name;
private int age;
//构造方法
public Foo1(String name,int age){
this.name = name;
this.age = age;
}
//方法
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
} public class Demo2 { } class User {
// 属性(private):姓名,年龄,职位,工资,地址,
// 邮箱,婚否(boolean marry)
private String name;
private int age;
private String job;
private double salary;
private String address;
private String email;
private boolean marry; // 构造方法:给每一个属性赋值
public User(String name, int age, String job, double salary,
String address, String email, boolean marry) {
this.name = name;
this.age = age;
this.job = job;
this.salary = salary;
this.address = address;
this.email = email;
this.marry = marry;
} // 方法:给每一个私有属性提供对应的get()/set()方法
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getJob() {
return job;
} public void setJob(String job) {
this.job = job;
} public double getSalary() {
return salary;
} public void setSalary(double salary) {
this.salary = salary;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
}
//boolean类型私有属性,get方法是以is开头
public boolean isMarry() {
return marry;
} public void setMarry(boolean marry) {
this.marry = marry;
} }

  4)默认的:系统默认提供访问控制修饰符,可以用来修饰类,属性,构造方法以及方法,被默认修饰类,属性,构造方法以及方法,在本包内任意访问,不同包之间无法访问。
  5)不同包之间创建对象,必须要先导入该对象所属的类
    import 包名.类名;
    案例:Demo3

import com.tarena.demo2.Person;
//import 包名.类名;
public class Demo3 {
public static void main(String[] args) {
Person p1 = new Person();
System.out.println(p1.name);
//被public修饰的属性,外界可以任意的访问。
// System.out.println(p1.age);
//被private修饰的属性,只能在本类的内部访问,外界
//无法访问。
// System.out.println(p1.address);
//被默认修饰的属性,只能在本包内访问,不同包之间无法
//访问。
}
}

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

public class Demo4 {
public static void main(String[] args) {
// Car car = new Car();
//不能使用抽象类直接的创建对象。
Benz benz =
new Benz("宝马","123456","白色",
"豪华版",200000.1);
benz.show();
benz.start();
benz.run();
benz.stop();
}
}
abstract class Car{
//属性
String name;
String no;
String color;
int speed = 0;
String type;
//构造方法
Car(String name,String no,String color,
String type){
this.name = name;
this.no = no;
this.color = color;
this.type = type;
}
Car(){ }
//非抽象方法
void show(){
System.out.println(name+","+no+","+
color+","+type+","+speed);
}
//抽象方法
abstract void start();
abstract void run();
abstract void stop();
}
class Benz extends Car{
//属性
double price;
//构造方法
Benz(String name,String no,
String color,String type,
double price){
this.name = name;
this.no = no;
this.color = color;
this.type = type;
this.price = price;
}
//方法
@Override
void run() {
System.out.println(name+"在行驶");
}
@Override
void start() {
System.out.println(name+"启动了");
}
@Override
void stop() {
System.out.println(name+"刹车了");
}
} public class Demo5 {
public static void main(String[] args) {
Midea midea =
new Midea("美的","壁挂式",
5000.33,29,"红色");
midea.show();
midea.hot(3);
midea.cool(5);
}
}
abstract class Kongtiao{
//属性
String name;
String type;
double price;
int degree;
//构造方法
Kongtiao(String name,String type,
double price,int degree){
this.name = name;
this.type = type;
this.price = price;
this.degree = degree;
}
Kongtiao(){
}
//方法
void show(){
System.out.println(name+","+type+","
+degree+","+price);
}
//抽象方法
abstract void cool(int degree);
abstract void hot(int degree);
}
//设计一个抽象类Kongtiao
//属性:名称,类型,价格,温度
//构造方法:1)给每一个属性赋值;2)空参构造方法
//方法:void show():输出每一个属性值
//抽象方法:
//abstract void cool(int degree):
//降低degree度
//abstract void hot(int degree):
//升高degree度
class Midea extends Kongtiao{
//属性:名称,类型,价格,温度,颜色
String color;
//构造方法
Midea(String name,String type,
double price,int degree,
String color){
super(name,type,price,degree);
this.color = color;
}
//方法
@Override
void cool(int degree) {
this.degree = this.degree - degree;
System.out.println(name+"降低"+degree
+"度以后,当前的温度是"+this.degree);
}
@Override
void hot(int degree) {
this.degree = this.degree + degree;
System.out.println(name+"升高"+degree
+"度以后,当前的温度是"+this.degree);
}
}

  3)不能直接使用抽象类来创建对象,因为抽象类中含有抽象方法(功能没有实现)。
  4)编写该抽象类的子类,该子类必须要重写抽象类中所有的抽象方法,实现功能,可以创建该抽象类的子类的对象。
  5)抽象类是一个用来设计的工具。
    企业开发中,把开发人员分成两类,一类是设计人员(项目经理),另一类是编码人员(程序员),由设计人员来设计抽象类或者接口,主要设计抽象方法,交给编码人员来编写该抽象类的子类,重写抽象方法,实现功能。
  6)抽象类侧重是抽象方法的设计,尽量少非抽象方法。

3 final

  最终的,最后的,可以用来修饰类,属性,方法
  1)使用final修饰的类,称为最终类。该类不能再被继承。
    案例:Demo6

public class Demo6 {

}
final class Zoo1{ }
/*
被final修饰的类,不能再被继承(不能再有子类)。
class Zoo2 extends Zoo1{
Math
}
*/

    sun公司设计的工具类,经常使用final进行修饰,比如String,
    Scanner,Math...但是在实际企业中,很少使用final修饰类。
  2)使用final修饰的方法,称为最终方法。该方法不能再被方法重写。
    案例:Demo7

public class Demo7 {

}
class Moo1{
final void f1(){
System.out.println("努力学习,2018快到了");
System.out.println("父类Moo1中编写" +
"方法f1()");
}
}
class Moo2 extends Moo1{
/*
被final修饰的方法,不能再被子类方法重写。
void f1(){
System.out.println("子类Moo2中重写" +
"父类Moo1中的方法f1()");
}
*/
}

  3)使用final修饰的属性,该属性的值一旦初始化以后,其属性值不会再被修改。
    案例:Demo8

public class Demo8 {
public static void main(String[] args) {
Yoo1 yoo = new Yoo1();
// yoo.num = 200;
//被final修饰的属性,其属性一旦初始化以后,其属性值
//不能再被修改。
}
}
class Yoo1{
final int num = 100;
void updateNum(){
// num = 200;
}
}

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

public class Demo9 {
public static void main(String[] args){
//类名.常量名
System.out.println(Eoo.MIN_PRICE);
System.out.println(Eoo.MAZ_SALARY);
System.out.println(Eoo.LOGIN_NAME);
//对象名.常量名
Eoo eoo = new Eoo();
System.out.println(eoo.MIN_PRICE);
System.out.println(eoo.MAZ_SALARY);
System.out.println(eoo.LOGIN_NAME);
}
}
class Eoo{
public static final
double MIN_PRICE = 2000000.1;
public final static
double MAZ_SALARY = 2000000.99;
public static final
String LOGIN_NAME = "admin";
}

  3) 访问常量:
    类名.常量名
    或者
    对象名.常量名

5 父类声明指向子类对象(了解)

  使用父类进行声明(定义)对象,使用子类的构造方法来创建对象。
  1)格式:
    父类 对象名 = new 子类构造方法
  2)使用父类声明指向子类对象这种方式所创建的对象,可以使用从父类中继承过来的方法。
    案例:Demo10

public class Demo10 {
//父类声明指向子类对象:使用父类来声明(定义)对象,
//调用子类的构造方法来创建对象。
//父类 对象名 = new 子类构造方法
public static void main(String[] args) {
Noo1 noo = new Noo2();
noo.f1();
//使用这种方式所创建的对象,可以访问从父类中继承
//过来的方法。
}
}
class Noo1{//父类
void f1(){
System.out.println("努力学习");
System.out.println("明天周五");
System.out.println("在父类Noo1中编写" +
"方法f1()");
}
}
class Noo2 extends Noo1{//子类 }

  3)使用父类声明指向子类对象这种方式所创建的对象,可以使用子类重写父类的方法。
    案例:Demo11

public class Demo11 {
public static void main(String[] args) {
//父类 对象名 = new 子类构造方法
Koo1 koo = new Koo2();
koo.f1();
//使用父类声明指向子类对象这种所创建的对象,可以访问
//子类重写父类的方法。
}
}
class Koo1{
void f1(){
System.out.println("父类Koo1中编写的" +
"方法f1()");
}
}
class Koo2 extends Koo1{
void f1(){
System.out.println("子类Koo2重写父类" +
"Koo1中的方法f1()");
}
}

  4)使用父类声明指向子类对象这种方法所创建的对象,不可以访问子类中单独编写的方法(非继承或者重写)
    案例:Demo12

public class Demo12 {
public static void main(String[] args) {
Coo2 coo = new Coo2();
coo.f1();
coo.f2();
//父类 对象 = new 子类构造方法
Coo1 coo2 = new Coo2();
coo2.f1();
// coo2.f2();
//使用父类声明指向子类对象这种方式所创建的对象,可以
//访问父类中声明(定义)过的方法(继承或者重写)。不能
//访问子类中单独编写的方法(非继承或者重写)
}
}
class Coo1{
void f1(){
System.out.println("父类Coo1中编写" +
"的方法f1()");
}
}
class Coo2 extends Coo1{
void f1(){
System.out.println("子类Coo2重写父类" +
"Coo1中的方法f1()");
}
void f2(){
System.out.println("子类Coo2中单独" +
"编写的方法");
}
}

  总结:使用父类声明指向子类对象这种方式所创建的对象,可以访问在父类中声明(定义)过方法,不能访问在子类中单独编写的方法。

最新文章

  1. node.js下使用RSA加密事例(windows)
  2. Entity Framework – (复数)Plural and (单数)Singular 表名Table names
  3. bzoj3594: [Scoi2014]方伯伯的玉米田--树状数组优化DP
  4. iOS问题处理:如何在Mac下显示Finder中的所有文件
  5. LPC1768之ISP
  6. express中使用 connect-flash 及其源码研究
  7. A Tour of Go If
  8. cortex m0 lpc1114的NVIC中断如何使用
  9. 帝国cms留言表模板修改
  10. C#中两个整数相除得到带小数点的结果
  11. MVC Razor标签
  12. OpenH264
  13. createThread和_beginthreadex区别
  14. 201521123042《Java程序设计》 第9周学习总结
  15. Maven第四篇【私有仓库、上传jar包、引用私服jar包、上传本地项目到私服】
  16. 小白的Python之路 day1 数据类型,数据运算
  17. Codeforces13E - Holes
  18. 自动提取文章摘要AutoSummary
  19. sequelize查询数据的日期格式化
  20. MySQL分区和分表

热门文章

  1. PJzhang:python基础进阶的10个疗程-two
  2. 安装neutron
  3. 【科普杂谈】IP地址子网划分
  4. C学习笔记-函数
  5. PHP学习(4)——数组的使用
  6. TP5之事务处理
  7. 安装部署FastDFS
  8. mysql在B-Tree上创建伪哈希索引
  9. pat L2--005 简单复习一下并差集
  10. Hibernate 数据库方言