欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

类和对象(一)

程序员文章站 2022-07-15 17:04:01
...

(一)以people为例子

/**
 * new:1)在堆上开辟内存
 *      2)调用构造函数:用来初始化成员变量
 *      没有构造函数,JVM默认给无参构造函数,给默认值
 * 类的构成: 1)成员变量(实例变量)(属性)
 *           2)构造方法(专门初始化成员变量)
 *           3)成员方法(实例方法)(行为)
 * 一个原文件只能对应一个public class People{  }  People.java必须和文件名相同
 * 局部变量:生命周期
 * 成员变量:生命周期 类中都有效
 * this关键字:指明当前对象,this.name
 * 如果局部变量的命名和成员变量的命名出现命名冲突,需要强制指明当前对象,this.
 *
 * 访问修饰符:
 * public 共有   private 私有    默认访问(包访问)
 *
 * 对象大小:对象存储位置
 * 对象大小:对象头+实例变量+内存填充
 */
public class People {
    //属性→实例变量
    private String name;//null
    private int age;//0 默认值
    //行为→实例方法
    public People(String name,int age){//初始化成员变量
          this.name=name;
          this.age=age;
    }

    public String getName() {
        return name;
    }
    public void setName(String name){

        this.name=name;
    }

    //行为→成员方法(实例方法)
    public void eat(){

    }
    public void sleep(){

    }
}

TestDemo

public class TestDemo {//专门存放main
    public static void main(String[] args) {
        People people1=new People("zs",14);//对象?堆 对象大小?
        //成员的访问方式 引用变量
        System.out.println(people1.getName());
        people1.setName("xyd");
        System.out.println(people1.getName());

        People people2=new People("lisi",20);
        System.out.println(people2.getName());

        People people3=new People("wangmazi",18);
        System.out.println(people3.getName());
    }

}

(二)以Bankcard为例子

public class BankCard {
     String name;
     int id;
     int password;
     int money;
     BankCard(String name,int id,int money,int password){
        this.name=name;
        this.id=id;
        this.password=password;
        this.money=money;
     }
     void saveMoney(int money){

         this.money=money+this.money;
     }

    /**
     * 查余额
     * @return
     */
     int getMoney(){

         return money;
     }
     void withdrawMoney(int money)
     {
         if(this.money>=money)
         {
             this.money-=money;
         }
     }
}

TestDemo

public class TestDemo {
    public static void main(String[] args) {
        BankCard bankCard1=new BankCard("工商银行",1234,500000,6666);
        System.out.println(bankCard1.id+bankCard1.name);
        bankCard1.saveMoney(10000);
        System.out.println(bankCard1.money);

        BankCard bankCard2=new BankCard("招商银行",5678,15000,5555);
        System.out.println(bankCard2.id+bankCard2 .name);
        bankCard2.withdrawMoney(100);
        System.out.println(bankCard2.money);

        BankCard bankCard3=new BankCard("建设银行",6666,2500,7777);
        System.out.println(bankCard3 .id+bankCard3 .name);
        bankCard3 .getMoney();
        System.out.println(bankCard3.money);
    }
}

(三)练习作业

public class MyArrayList {
    private int[]element;
    public MyArrayList(int[] element) {//初始化成员变量
        this.element = element;
    }

    public void addHead(int value){
       int count=getCount(element);
       int arrlength=element.length;//原数组长度
        if(count==element.length){//数组满
         element= Arrays.copyOf(element,element.length+1);
        }
            //数据移动,腾0下标
        int index;
        for(index=arrlength-1;index>=0;index--){
            element[index+1]=element[index];
        }
        //index == -1
        element[index+1] = value;
      //element[0] = value;
    }
    public int[] getElement(){
        return element;
    }
    public int getCount(int[]element){
        int count=0;
        for(int i=0;i<element.length;i++){
            if(element[i]!=0){
                count++;
            }
        }
        return count;
}
     public void addTail(int value){
       int count=element.length;
       element= Arrays.copyOf(element,element.length+1);
       element[count]=value;
     }
    public void deletHead(){

          for(int i=0;i<element.length-1;i++){
              element[i]=element[i+1];
          }
        element=Arrays.copyOf(element,element.length-1);
    }
    public void deleteTial(){
       element=Arrays.copyOf(element,element.length-1);
    }
    }

TestDemo

public class TestDemo {

    public static void main(String[] args) {
        MyArrayList myArrayList1=new MyArrayList(new int[]{1, 2, 3, 4, 5});
        myArrayList1.addHead(0);
        System.out.println(Arrays.toString(myArrayList1.getElement()));

        MyArrayList myArrayList2=new MyArrayList(new int[]{0,1,2,3,4});
        myArrayList2.addTail(5);
        System.out.println(Arrays.toString(myArrayList2.getElement()));

        MyArrayList myArrayList3=new MyArrayList(new int[]{11,0,1,2,3,4,5});
        myArrayList3.deletHead();
        System.out.println(Arrays.toString(myArrayList3.getElement()));

        MyArrayList myArrayList4=new MyArrayList(new int[]{0,1,2,3,4,5,6});
        myArrayList4.deleteTial();
        System.out.println(Arrays.toString(myArrayList4.getElement()));
    }
}

输出结果:
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]

相关标签: java