Java-----面向对象的三个特征

面向对象的三个特征是:封装、继承、多态

一、封装

  【对属性的封装】

    封装的目的:只能通过规定的方法访问数据,防止不合法的输入

    1.将属性进行私有化处理,不允许外部程序直接访问,并且对外提供统一的方法来设置和读取私有属性

    2.实现访问控制,限制属性访问

 1 public  class  Test{
 2   //私有化处理
 3   private String name;
 4   private String sex;
 5   private int age;
 6 
 7   //空参构造与全参构造
 8   public A() {
 9     super();
10   }
11   public A(String name, String sex, int age) {
12     super();
13     this.name = name;
14     this.sex = sex;
15     this.age = age;
16   }
17 
18 //设置get、set方法
19   public String getName() {
20     return name;
21   }
22   public void setName(String name) {
23     this.name = name;
24   }
25   public String getSex() {
26 
27     return sex;
28   }
29   public void setSex(String sex) {//对输入进行控制
30     if(sex=="男"){
31         this.sex = sex;
32     }else if(sex=="女"){
33         this.sex = sex;
34     }else{
35         System.out.println("输入不合法");
36         this.sex="男";
37     }
38   }
39   public int getAge() {
40     return age;
41   }
42   public void setAge(int age) {
43     if(age>0&&age<100){
44         this.age=age;
45     }else{
46         System.out.println("年龄不合法");
47         this.age=-1;
48     }
49   }
50 //方法
51   public void intro(){
52     System.out.println("我是"+this.name+",我是"+this.sex+"生, 
53   我"+this.age+"岁");
54   }
55 
56 } 

重载:与返回值类型,访问修饰符无关

特点:方法名相同,参数列表不同(参数个数、参数类型,参数的前后顺序)

典型:空参构造和全参构造

  

二、继承:优化代码的一种思路

用 extends 关键字

父类

public class Fruit {
    private double weight;
    
    public Fruit(double weight) {
        super();
        this.weight = weight;
    }

    public void intro(){
        System.out.println("我是水果,"+weight+"克");
    }

}
子类

public class Apple extends Fruit{

    public Apple(double weight) {
        super(weight);
        // TODO Auto-generated constructor stub
    }
    

}

子类访问父类成员,使用super关键字,但是仍不能访问父类私有的属性和方法

重写

特点:在子类中,重写方法要求方法名相同、参数列表相同、返回值相同、访问控制符不能比父类更加严格

重写和重载的区别:http://www.cnblogs.com/1960366876tZ/p/8685965.html

抽象类:用abstract修饰的类   

特点:不能被实例化

抽象方法:用abstract修饰,并且没有方法体

子类要继承抽象类,必须要重写父类所有的抽象方法,除非子类也是抽象类

抽象方法必须要写在抽象类中

final关键字

1.final修饰的类为最终类,不能被继承

2.final修饰的方法为最终方法,不能被重写

3.final修饰的变量为常量,不可被修改 

static关键字:可以用来修饰属性、方法、代码块

1.用static修饰的方法和属性为静态方法和静态属性,调用时使用类名直接调用即可

2.静态方法和静态属性也叫类方法和类属性,在类装载的时候就直接声明,

  因此,静态方法中不能调用非静态属性和方法(这个时候还没有对象 ,自然也就没有成员属性和方法)

  所以也不能使用this 和 super  

3.由于类属性和类方法时属于类的所以类装载的时候只会产生一份,后续使用该类实例化的多个对象都将共用这一个静态变量

三、多态

程序中的多态是指,同一个引用类型 ,由于使用不同的对象实例,而执行不同的操作

实现多态的条件:

1.子类继承父类

2.子类重写父类的方法

3.父类引用指向子类

public abstract class Instrument {
    
    private String brand;
    private double weight;
    public Instrument(String brand, double weight) {
        super();
        this.brand = brand;
        this.weight = weight;
    }
    public Instrument() {
        super();
    }
    
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    
    //抽象方法
    public abstract  void play();

}
//子类1
    private double size;
    public Piano(){
        
    }
    public Piano(double size) {
        super();
        this.size = size;
    }


    public double getSize() {
        return size;
    }

    public void setSize(double size) {
        this.size = size;
    }
    
    public void play(){
        System.out.println("演奏钢琴");
    }

}
//子类2
public class Violin extends Instrument {

    private double length;
    public Violin(){
        
    }
        public Violin(double length){
               super();
               this.length=length;
        
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
    public void play(){
        System.out.println("演奏小提琴");
    }
    
}
public class Artist{
    public void make(Instrument i){
        i.play();
    }
}
//测试类
public class Test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Artist a=new Artist();
            
        //声明的是父类,指向的是子类的一个对象    
        Instrument i=new Piano();
        a.make(i);
    }

}
原文地址:https://www.cnblogs.com/1960366876tZ/p/8747985.html