面向对象chapter2

 1.方法重载
    1.1方法重载:方法名一样,参数列表不一样
            注意:重载返回值类型和访问修饰符无关。
    
     2.static和final
    static:静态图
        用static修饰的属性,直接可以类名,方法名访问
    final:最终的
        用final修饰的属性,他的值初始化后,不能再改变

    后++,先把本身的值作为表达式的值,然后本身+1
    例:a++
    前++,先把本身+1,然后再把值作为表达式的值。
    例:++a
    后--,先把本身的值作为表达式的值,然后本身-1
    前--,先把本身-1,然后再把值作为表达式的值。

          static,非private修饰    非static,private修饰
    属性:类属性,类变量        实例属性,实例变量
    方法:类方法            实例方法

            类名.属性    对象.属性
    调用方式:    类名.方法()    对象.方法()
            对象.属性    
            对象.方法()
    归属:        类        单个对象

public class Day02 {
    String name;
    int health=99;
    int love=99;
    String sex;
    //常量
    //public static final 常量名=常量值;
    
    //注意:1.final最终的,修饰的变量不能被修改
    //2.变量名:所有字母大写,多个单词用_分隔
    public void print(){
        System.out.println("宠物自白:我的名字叫"+name+",健康值"+health+",亲密度"+love+",性别"+sex);
    }
}

public class Day03 {
        //品种
        private int strain;
        //年龄
        private int age = 8;
        //昵称
        private String name = "小灰灰";
        //健康
        private int health = 100;
        //亲密
        private int love = 100;
        //打印信息
    //构造方法(默认构造方法)
        public Day03(String name,int strain){
            this.name = name;
            this.strain = strain;
        }
        public Day03(){
            this.name="畹町";
            this.age=18;
            this.love=99;
            this.health=100;
            System.out.println("-----执行dog构造方法-----");
        }
        public void print(){
            System.out.println(this.name+","+this.age+","+this.strain);
    }

}

public class TestDay03 {

    public static void main(String[] args) {
        Day03 w = new Day03();
        w.print();
    }

}


final class Bus extends MotoVehlele {
    int Seatcount;
    public Bus(){
        
    }
    public Bus(String no,String brand,int Seatcount ){
        super(no,brand);
        this.Seatcount=Seatcount;
    }
    public int getSeatcount() {
        return Seatcount;
    }
    public void setSeatcount(int seatcount) {
        Seatcount = seatcount;
    }
    public int CalRent(int indays,int type){
        int a=0;
        if(type<=16){
            a = indays*800;
        }
        if(type>16){
            a = indays*1500;
        }
        return a;
    }
}


public abstract class MotoVehlele {
    String no;
    String brand;
    public MotoVehlele(){
        
    }
    public MotoVehlele(String no,String brand){
        this.no=no;
        this.brand =brand;
    }
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public abstract int  CalRent(int indays,int type);
    
}





原文地址:https://www.cnblogs.com/wangjinshabi250/p/7019713.html