JAVA学习基础

JAVA学习笔记(7)

定义常量:private final static int INIT(大写)=10;

函数重载:
条件:(同一个类)1.函数名相同 2.参数列表不同(个数,类型)

this关键字用法:
1)this指向当前对象
实例成员变量跟对象有关,有隐含的this引用。一个对象有一份实例变量
2)this调用构造方法
注意:
(1)this()位于当前构造方法有效代码的第一行
(2)构造方法不能相互调用
(3)一个构造函数不能调用多个构造函数)

static关键字:
People p1=new People,p2=new People,p3= new People;

静态变量和实例变量的区别:

1.实例变量和对象有关,一个对象(一次new)对应一份成员变量
this->指向当前对象
静态变量和类有关,跟对象无关,当前的静态变量没有隐含的this引用
存储位置:实例变量(堆new) 静态变量(方法区)
调用方式:实例变量:引用变量. People p = new People; p.name;
静态变量:类名. Arrays.copyOf();
静态方法:
1.静态方法不能提供this引用
2.成员方法里面包含隐含的this
1.2 -> 静态方法不能直接调用非静态方法,必须通过对象调用
成员方法能不能调用静态方法:可以
static:有没有this,跟对象有无关系

eg1:数组的头插,尾插,扩容,删除等

public class MyArrayList {
    private int[] element;
    private int size;//有效个数
    private final static int INIT = 10;//常量
    public MyArrayList(){
        this(INIT);
    }
    public MyArrayList(int num){
        this.element = new int[num];
        size = 0;
    }
    private void grow(){
        element = Arrays.copyOf(element,element.length+(element.length>>1));
    }
    private void esureCapacity(){
        if (size == element.length){
        grow();
        }
    }
    public void addTail(int value){
        //是否需要扩容
        esureCapacity();
        element[size] = value;
        size++;
    }
     public void addHead(int value){
         esureCapacity();
         for(int i =size-1;i>=0;i--){
             element[i+1]=element[i];
         }
         element[0] = value;
         size++;
     }
     public void  deleteValue(int value){
        for(int i =0;i<size;i++){
            if (element[i] == value){
                //移动数据进行覆盖
                for(int j=i+1;j<size;j++){
                    element[j-1]=element[j];
                }
                size--;
                i--;
            }
        }
     }
     public void   deleteIndex(int index){
        if (index < 0 || index >= size){
            return;
        }
         for(int j=index+1;j<size;j++){
             element[j-1]=element[j];
         }
         size--;
     }
     public void  deleteRange(int beginIndex,int endIndex){
        if(beginIndex > endIndex || beginIndex<0|| beginIndex>=size ||endIndex<0){
            return;
        }
        if (endIndex>=size){
            endIndex = size-1;
        }
        int differ = endIndex-beginIndex+1;
        //数据移动
         for (int j = endIndex+1;j<size;j++){
             element[j-differ] = element[j];
         }
         size-=differ;
     }
     public void show(){
        for (int i=0;i<size;i++){
            System.out.print(element[i]+" ");
        }
     }
}
public class TestDemo {
    public static void main(String[] args) {
        MyArrayList my = new MyArrayList();
        my.addTail(1);
        my.addTail(2);
        my.addTail(3);
        my.addHead(2);
        my.addHead(2);
        my.addHead(2);
        my.addHead(2);
//      my.deleteValue(2);//删除2值
//      my.deleteIndex(0);//删除0号下标
//      my.deleteRange(5,6);//删除范围下标元素
        my.show();
}

eg2:猜拳游戏

public class Test1 {
    public static void main(String[] args) {
        Game game = new Game();
        game.start();
        /*
        猜拳:3次   打印People赢了 比分:2:1
          游戏开始:
          请出拳: 石头
          对方出拳: 剪刀
          People赢  1

         对方赢:  1
         People 赢  1+1    2
         */
    }
}
public class Computer {
    private String name = "computer";
    private int score;
    public Computer(){
        score = 0;
    }
    public String fist(){
        Random random=new Random();
        int n=random.nextInt(3)+1;//[1,3]
        if(n == 1){
            return "石头";
        }else if (n == 2){
            return "剪刀";
        }else {
            return "布";
        }
    }
    public void addScore(int score){
        this.score +=score;
    }
    public int getScore(){
        return score;
    }
}
public class People {
    private String name = "people";
    private int score;
    public People(){
        score = 0;
    }
    public String fist(){
        Scanner scanner = new Scanner(System.in);
        String fist = scanner.next();
        return fist;
    }
    public void addScore(int score){
        this.score +=score;
    }
    public int getScore(){
        return score;
    }
}
public class Game {
    private People people;
    private Computer computer;
    public String a ="剪刀";
    public String b = "石头";
    public String c = "布";
    public Game(){
        people = new People();
        computer = new Computer();
    }
    public void start() {
        int count = 0;
        System.out.println("游戏开始");
        while(count<3) {
            System.out.println("请出拳:");
            String pFist = people.fist();
            String cFist = computer.fist();
            System.out.println("对方出:" + cFist);
            //People赢
            if (pFist.equals("石头") && cFist.equals("剪刀") ||
                    pFist.equals("剪刀") && cFist.equals("布") ||
                    pFist.equals("布") && cFist.equals("石头")) {
                System.out.println("people赢");
                people.addScore(1);
            } else if (pFist.equals("剪刀") && cFist.equals("石头") ||
                    pFist.equals("布") && cFist.equals("剪刀") ||
                    pFist.equals("石头") && cFist.equals("布")) {
                System.out.println("computer赢");
                computer.addScore(1);
            } else {
                System.out.println("平局");
            }
            count++;
        }
        int pScore = people.getScore();
        int cScore = computer.getScore();
        if(pScore>cScore){
            System.out.println("people最终获胜! people:computer="+pScore+":"+cScore);
        }else if(pScore<cScore){
            System.out.println("computer最终获胜! people:computer="+pScore+":"+cScore);
        }
        System.out.println("三局结束");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值