java面向对象总结04

成员变量初始化值

1.Java中所有的变量必须先声明,后赋值才能使用
2.Java中的成员变量,在创建对象的时候都会执行一次初始化值
3.基本数据类型默认值都是0包括Boolean->false
4.引用数据类型:null
null表示空,什么都没有,占位

package com.oop;
public class oopDay18 {
    int age;
    byte b;
    short s;
    long l;
    double d;
    float f;
    boolean bool;
    String str;
}



package com.oop;
//成员变量的初始化值

public class oopDay17 {
    public static void main(String[] args) {
        oopDay18 x = new oopDay18();
        System.out.println("byte="+x.b);//0
        System.out.println("short="+x.s);//0
        System.out.println("int="+x.age);//0
        System.out.println("long="+x.l);//0
        System.out.println("double="+x.d);//0.0
        System.out.println("boolean="+x.bool);//false
        System.out.println("float="+x.f);//0.0
    }

20.object

万事万物皆为对象
所有的东西都是对象
在Java中所有的类都要继承object
object是一个类,所有类的根
我们写的类即使不写继承关系,那么默认也会继承object

package com.oop;

public class oopDay18 extends Object {}
//按住Ctrl点Object可以查看object的代码

equals和==

==判断左右两端的数据是否一致(判断地址值)
equals:object类提供的一个方法,用来判断两个对象是否相等(判断内容)
equals可以重写
字符串的判断一定要用equals来判断

public class Cat {
    String name;
    String color;
    public Cat(String name,String color){
        this.color = color;
        this.name = name;
    }
    public boolean equals(Cat c){
        if (this.color == c.color){
            return true;
        }else{
            return  false;
        }
    }

    public static void main(String[] args) {
        Cat c1 = new Cat("小花","红色");
        Cat c2 = new Cat("小花","红色");
        System.out.println(c1.equals(c2));
    }
}

public class Test {
    public static void main(String[] args) {
        String str3 = new String("杠精");
        String str4 = new String("杠精");
        System.out.println(str3.equals(str4));//true
        System.out.println(str3 == str4);//false
    }
}

小练习:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
      String usrename = "wuyanze";
      String password = "12345";
      Scanner sc = new Scanner(System.in);
      System.out.println("请输入账户");
      String usre = sc.nextLine();
      System.out.println("请输入密码");
      String pass = sc.nextLine();
      if (usre.equals(usrename)&&pass.equals(password)){
          System.out.println("输入正确");
      }else{
          System.out.println("输入错误");
      }
    }
}

toString

toString()对一个对象的字符串的表示形式
JAVA官方推荐你重写使用这个方法,默认的那个太丑了

package com.xyq.entity;

public class Cat {
    String name;
    String color;

    @Override
    public String toString() {
        return "我的猫叫"+this.name+"是"+this.color;
    }

    public Cat(String name, String color){
        this.color = color;
        this.name = name;
    }

    public static void main(String[] args) {
        Cat c= new Cat("小花","绿色");
        System.out.println(c);
    }
}

instanceof关键字

instanceof:判断xxx对象是否是xxx类型的

package com.xyq.entity;

public class Cat extends Animal {

    public static void main(String[] args) {
       Animal c = new Cat();
       if (c instanceof Cat){
           System.out.println("我是猫,玩一玩");
       }else {
           System.out.println("我不是,你走开");
       }

    }
}

参数传递问题:

值传递:把变量的值作为参数进行传递
引用传递:直接把变量作为参数进行传递

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值