天天看点

第六天

创建对象

面对对象:以类的方式组织代码,以对象组织(封装)数据。

抽象

三大特性:封装性、继承性,多态性

初始化对象使用new关键字创建对象

类中的构造方法:1、必须和类的名字相同。2、必须设有返回类型,也不能写void

public class mate{

  String name;

  int age;

  int sno;

}

=====================================================================

public class Student {

  public static void main(String [] agrd){

     mate xiaoMin= new mate();//初始化

     mate xiaohong=new mate();

    xiaoMin.name ="xiaomin";

    xiaoMin .age =19;

    xiaoMin .sno =1213;

    xiaohong .name ="xiaohong";

    xiaohong .age =18;

    xiaohong .sno =1214;

    System.out.println(xiaoMin .name ) ;

   }

class Qishi {

  private int leve;

  private String name;

  private int hp;

  private int mp; //protected,private,public ==>面向对象程序设计 都有 //函数的 重载和重写 //不带参数的构造函数 => public + 类名

  public Qishi() {} //带参的构造函数

  public Qishi(int leve,String name,int hp,int mp) {

    this.leve = leve;

    this.name = name;

    this.hp = hp;

    this.mp = mp;

} //构造方法

  public void setName(String name) {

  }

  public String getName() {

    return this.name;

  public void setLeve(int leve) {

  public int getLeve() {

    return this.leve;

  public void setHp(int hp) {

  public int getHp() {

    return this.hp;

  public void setMp(int mp) {

  public int getMp() {

    return this.mp;

这里如果没有写构造方法的话系统是会默认生成构造函数的。

对象的应用:

引用类型:基本类型

对象是通过引用来操作的:栈-->堆

属性:字段field 成员变量

默认初始化:

数字:0 0.0

char:u000

布尔值:false

引用:null

修饰符 属性类型 属性名=属性值

对象的创建和使用:

必须使用new

对象属性、对象方法

继续阅读