JAVA 十五 构造方法,静态常量

JAVA 十五 构造方法,静态常量

标签: JAVA


1.构造方法

Student stu=new Student();
Student();类名(构造方法);
在类中

public class Student{
    double java,c,db;
    double height;
    public Student(){//构造方法
    }
}
  • 特点一 没有返回值类型
  • 特点二 方法名和类名必须相同
  • 系统默认提供一个无参的构造方法,但是一旦自己定义了构造方法,那么无参的构造方法就没有了

2.方法的重载:发生在同一个类文件里面,

  • 方法名相同,包括大小写
  • 参数列表不同(参数个数,参数类型都可以不同)
  • 方法的重载与返回值类型没有关系

3.静态常量

  • static 可以修饰方法、属性、代码块///用static属性修饰的方法随着类的加载而加载,一旦有了类其中的static方法就有了
  • 调用方法:类名.方法或者属性
  • 静态方法里面只能使用静态的属性
  • 静态方法里面不可以调用非静态的方法

在方法里面定义的局部变量除了类型外不能加任何的修饰符

final 最终的 可以修饰属性、方法、类;不可以改变的用final

  • final修饰的属性会变成常量 定义的时候必须初始化值,而且不可改变
定义属性一般需要私有化

4.封装

  • this本类对象:用法this.java,不用生成对象 this 即是对象
  • 类里面定义的属性必须私有化操作
  • 在构造方法中调用构造方法 this();

封装类练习

import java.util.Scanner;
public class TextDogPenguin {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int a=0;
        System.out.println("欢迎您来到宠物商店");
        System.out.println("请输入要领养宠物的名字");
        String name=input.next();
        System.out.println("请输入您要领养的宠物类型1.狗狗2.企鹅");
        String type;
        a=input.nextInt();
        System.out.println("请输入品种");
        String strain=input.next();
        System.out.println("请输入的健康值(默认60)");
        int healthy=input.nextInt();
        if(healthy>=100||healthy<=0){
            healthy=60;
        }
        System.out.println("请输入亲密度");
        int love=input.nextInt();
        if(a==1){
            Dog gou=new Dog(name,healthy,love,strain);
            gou.show();
        }else{
            Penguin pen=new Penguin(name,healthy,love,strain);
            pen.show();
        }
    }
}
import java.util.Scanner;
public class Dog {
    Scanner input=new Scanner(System.in);
    private String name,type;
    private int love,healthy;

/*  public void setName(String name){
*       this.name=name;
    }*/
    private void setHealthy(int healthy){
        this.healthy=healthy;
    }
    public Dog(String name,int health,int love,String type){
        this.name=name;
        this.healthy=health;
        this.love=love;
        this.type=type;
    }
     public void show(){
         System.out.println("狗\t名字:"+name+"健康度:"+healthy+"\t亲密度:"+love+"\t品种:"+type);
         }
/*  public void setType(String type){
 *      this.type=type;
 *  }
 *  public void setlove(int love){
 *      this.love=love;
 *  }
 *  public String getName(){
 *      return name;
 *  }
 *  public String getType(){
 *      return type;
 *  }
 *  public int getlove(){
 *      return love;
 *  }
 *  public int gethealthy(){
 *      return healthy;
 *  }
 *  public void show(){
 *      System.out.println("健康度:"+gethealthy()+"\t亲密度:"+getlove()+"\t名字:"+getName()+"\t品种:"+getType());
 *  }
 *  public Dog(){
 *  }
    }*/
}

动物属性封装管理练习

import java.util.Scanner;
public class Penguin {
    Scanner input=new Scanner(System.in);
    private String name,type;
    private int love,healthy;
    private void setHealthy(int healthy){
        this.healthy=healthy;
    }
    public Penguin(String name,int health,int love,String type){
        this.name=name;
        this.healthy=health;
        this.love=love;
        this.type=type;
    }
     public void show(){
         System.out.println("企鹅\t名字:"+name+"健康度:"+healthy+"\t亲密度:"+love+"\t品种:"+type);
         }
    /*public void setName(String name){
        this.name=name;
    }
    public void setHealthy(int healthy){
        this.healthy=healthy;
    }
    public void setType(String type){
        this.type=type;
    }
    public void setlove(int love){
        this.love=love;
    }
    public String getName(){
        return name;
    }
    public String getType(){
        return type;
    }
    public int getlove(){
        return love;
    }
    public int gethealthy(){
        return healthy;
    }
    public void show(){
        System.out.println(this+"健康度:"+gethealthy()+"\t亲密度:"+getlove()+"\t名字:"+getName()+"\t品种:"+getType());
    }
    public Penguin(){
    }*/
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值