JAVA学习笔记

视频链接:https://www.bilibili.com/video/BV1Rx411876f?p=153
本人系统:win10
使用软件:IntelliJ IDEA 2020.3 x64

基本知识点:

【命名】

遵守驼峰命名方式 (SystemService 、UserService 、CustomerService)
*类名、接口名:首字母大写,后面每个单词首字母大写。
*变量名、方法名:首字母小写,后面每个单词首字母大写。
*常量名:全部大写

【注释】

单行注释://
多行注释:/* * /
文档注释:/** * /

【数据类型】
在这里插入图片描述
类型转换:

byte i = 1 ;
int j = 10245;
i = (byte) j;

【运算符】

! 非
^ 异或
&& 短路与

【字符串连接】

a=100; b = 200;
System. out. print1n(a + ”+" + b + ”=" + (a+b));

【控制语句】
if

if( x == 10 ){
System.out.print(“Value of X is 10”);
}
else if( x == 20 ){
System.out.print(“Value of X is 20”);
}
else if( x == 30 ){
System.out.print(“Value of X is 30”);
}
else{
System.out.print(“这是 else 语句”);
}

for

for(int number=1;number<=5;number++){
result*=number;
}

while

while(i <= 10) {
n=n*i;
i++;
}

【helloword解释】

	// public表示公开的,在任何位置都是可以访问的
    // static表示静态的,使用“类名.”的方式即可访问,不需要创建对象,就可以调用main方法
    // void表示main方法执行结束之后不返回任何值
    // main是main方法的方法名
    // (String[] args) 是main方法的形式参数列表
class HelloChina{ 
	public static void main(String[] args){ 
		System.out.println("Hello!");  
	}
}

1、类体中不可以写java语句,声明变量除外
2、Java语句要加分号
3、main 主方法,是程序运行的入口
4、public class 和class的区别:
一个java源文件当中可以定义多个class
一个java源文件当中publ1c的class不是必须的
一个class会定义生成- -个xxx . class字节码文件
一个java源文件当中定义公开的类的话,只能有一个,并且该英名称必须和java源文件名称一致。

类和对象

public class OOTest {
	public static void main(String[] args) {
		 student s = new student(); // 创建
		 s.addr = new address(); // 创建
		 int a = s.age;
		 s.sex = true;
		 boolean se = s.sex;
		 
		 System.out.println("年龄 = " + a );
		 System.out.println("性别 = " + se );
		 System.out.println("姓名 = " + s.name );
		 System.out.println( s.name + " is from " + s.addr.country);
	}
}

// 定义类
class  student{
	int age;
	string name ;
	boolean sex;
	String name = "zyy";
	address addr ; // address 类型的变量
}

class address{
	String country = "China";
	String city;
	String street;
	int code;
}

方法

package com.atz;

public class Test01 {
    public static void main(String[] args){
        User u = new User(20); // u 不是变量 ,而是指针
        add2(u); // 传的是地址,改变原有的值
        System.out.println("main-->"+u.age);
    }

    public static void add(int i) {
        i ++; // 传的是字面值,只改形参,不改变原来的 i
        System.out.println("add -->"+i);
    }

    public static void add2(User u) {
        u.age ++;
        System.out.println("add -->"+u.age);  // i 是局部变量,不改变main里的i
    }
}

class User{
    int age;

    // 构造方法
    public User(int i){
        age = i;
    }
}

方法重载

overload
在同一个类当中方法名相同,
参数列表不同:类型、顺序、个数
方法的返回类型可以相同也可以不相同。

public class Demo {
	public void add(){
		//method body
	}

	public void add(int a){
		//method body
	}
	
	public int add(int a,int b){
		//method body
		return 0;
	}
}

访问控制权限

1、修饰类、变量、方法
2、
public 公开的,任何位置都可以访问
private 私有的,只在本类中访问
protected 同包、子类可以访问
缺省的 同包可以访问
3、修饰符的范围:
private <缺省< protected < public

final 关键字

1、final是-个关键字,表示最终的,不可变的。
2、final修饰的类无法被继承 修饰的方法无法被覆盖
3、final修饰的变量一旦手动赋值之 后,不可重新赋值
4、final修饰的实例变量,必须手动赋值
5、final修饰的引用,public static final String GUO_JIA = “China” , 作常量用,静态(没对象)不可变

public class FinalTest {
    public static void main(String[] args){
        final int k = 100;
        // k = 200; // 报错
        // final int i; // 报错,要给初始值

        Data d = new Data();
        // 变成垃圾数据。等待垃圾回收器回收
        d = new Data(2020,5,12);
        final Data d2 = new Data(2020,50,13); // 地址不能变
        d2.test = 50;
        System.out.println(d2.test); // 但是能改变内容
    }
}
public class Data {
    private int year;
    private int month;
    private int day;
    public int test = 1;
    protected int pro = 2;
    int quesheng = 2;

    public Data(int year, int month, int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public Data(){
        /* 代码重复,调用
        this.year = 1997;
        this.month = 11;
        this.day = 3;
        */
        // this方法只能在第一行
        this(1997,11,3); // 不会创建新的对象,记住就行
    }

    public void print(){
        System.out.println(this.year + "-" + this.month + "-" +this.day );
    }

    public void show(){
        System.out.println("Father class data is working!!!");
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }
}



封装、继承、多态

1、封装

package com.atz;
/* 面向对象的封装性 */
public class Student01 {
    // 属性私有化,set get
    private int id;
    private int age ;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    void setAge(int a){
        if(a < 0 | a> 150){
            System.out.println("输入的年龄错误");
            return;
        }
        age = a;
    }

    int getAge(){
        return age;
    }
}

封装后需要get、set函数来获取、改变内部变量
在这里插入图片描述
在这里插入图片描述
this 关键字
this 用法综合测试

package com.atzyy1222;

public class Test {
    int i = 10;

    public static void main(String[] args){
        // 调用method1
        // 完整方式
        // 省略方式

        // 调用method2 ???
        // 完整方式
        // 省略方式, 无
    }


    public static void method1(){
        // 调用 jump
        // 完整方式
        // 省略方式

        // 调用 run, 实例方法先实例化
        // 完整方式 ?
        // 省略方式,无

        // 访问i
        // 完整方式
        // 省略方式
    }

    public void method2(){
        // 调用 jump
        // 完整方式
        // 省略方式

        // 调用 run
        // 完整方式
        // 省略方式


        // 访问i
        // 完整方式
        // 省略方式

    }

    public static void jump(){
        System.out.println("static jump");
    }

    public void run(){
        System.out.println("no static run");
    }
}

this 用法综合答案

package com.atz1222;

public class TestAnswer {
    int i = 10;

    public static void main(String[] args){
        // 调用method1
        // 完整方式
        TestAnswer.method1();
        // 省略方式
        method1();

        // 调用method2 ???
        // 完整方式
        TestAnswer t = new TestAnswer(); // 没有对象,创建对象
        t.method2();
        // 省略方式, 无
    }

    public static void method1(){
        // 调用 jump
        // 完整方式
        TestAnswer.jump();
        // 省略方式
        jump();

        // 调用 run, 实例方法先实例化
        // 完整方式 ?
        TestAnswer t = new TestAnswer();
        t.run();
        // 省略方式,无

        // 访问i
        // 完整方式
        System.out.println(t.i);
        // 省略方式
    }

    public void method2(){
        // 调用 jump
        // 完整方式
        TestAnswer.jump();
        // 省略方式
        jump();

        // 调用 run
        // 完整方式
        this.run(); // 本身有对象
        // 省略方式
        run();

        // 访问i
        // 完整方式
        System.out.println(this.i);
        // 省略方式
        System.out.println(i);
    }

    public static void jump(){
        System.out.println("static jump");
    }

    public void run(){
        System.out.println("no static run");
    }
}

this用法2

public class Data {
    private int year;
    private int month;
    private int day;
    public int test = 1;
    protected int pro = 2;
    int quesheng = 2;

    public Data(int year, int month, int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public Data(){
        /* 代码重复,调用
        this.year = 1997;
        this.month = 11;
        this.day = 3;
        */
        // this方法只能在第一行
        this(1997,11,3); // 不会创建新的对象,记住就行
    }
}

this可以用在哪里:
1、可以使用在实例方法当中,代表当前对象[语法格式: this.]
2、可以使用在构造方法当中,通过当前的构造方法调用其它的构造方法[语法格式: this (实参) ;]

无static = 实例 = 对象参与
静态中无法直接访问实例,也没有this
静态可直接通过类名去调用

2、继承

1、实质:代码复用
2、继承语法格式:
[修饰符列表] class 类名extends父类名{
类体-属性+方法
}
3、不支持继承:私有的、构造方法
4、java语言中假设一个类没有 显示的继承任何类,该类默认继承JavaSE库当中提供的java。lang。object类。

方法覆盖

override/overwrite
当父类中的方法已经无法满足当前子类的业务需求,子类有必要将父类中继承过来的方法进行重新编写,
【注意】
1、返回值类型相同,方法名相同,形參列表相同,建议复制粘贴
2、访问权限不能更低,只能更高
3、不能继承的不能覆盖
4、静态方法不存在覆盖

public class OverrideTest {
    public static void main(String[] args){
        Animal a = new Animal();
        Cat b = new Cat();
        MeiDuan c = new MeiDuan();
        a.move();
        b.move();
        c.move();
    }
}
public class Animal {
    public int age = 2;
    public void move (){ // static方法不能覆盖
        System.out.println("动物在移动");
    }
    public void eat(){
        System.out.println("mia mia mia");
    }
}
public class Cat extends Animal{
    public void move (){
        System.out.println("小猫咪在走路o(* ̄▽ ̄*)o");
    }

    // 子类特有的行为
    public void catchRat(){
        System.out.println("小猫咪在抓老鼠");
    }

    public void eat(){
        System.out.println("猫:mia mia mia");
    }
}
public class MeiDuan extends Cat{
    @Override
    public void move() {
        // super.move();
        System.out.println("美短GOGOGO!!");
    }
}

结果:

动物在移动
小猫咪在走路o( ̄▽ ̄)o
美短GOGOGO!!

3、多态

向上转型(upcasting),子变父
向下转型(dowmcasting),父变子

public class DuoTaiTest {
    public static void main(String[] args) {
        Animal a = new Cat(); // 向上转型
        a.move(); // cat的move,内容是Cat,框架是Animal
        // a.catchRat();  无子类特有方法

        Cat c1 = (Cat) a;// 向下转型,存在隐患,可以编译但无法运行 
        				 //如 bird 不能转成 cat,无继承关系。
        c1.move();
        c1.catchRat();

        Animal a2 = new Bird();
        
        // instanceof 可避免向下转型出现的 ClassCastException
        if(a2 instanceof Cat){ // a是Cat的实例
            Cat c = (Cat) a2;
            c.catchRat();
        }
        else if (a2 instanceof Bird){
            Bird b = (Bird) a2;
            b.fly();
        }
    }
}

两个对象

public class DuoTaiTest2 {
    public static void main(String[] args){
        Master m = new Master();
        Cat c = new Cat();
        m.feed(c);

        Bird b = new Bird();
        m.feed(b);

        System.out.println(b instanceof Animal);
    }
}
public class Master {
    // Master和Cat、Bird这两个类型的关联程度很强,耦合度很高,扩展力差。
    /*
    public void feed(Cat c) {
        System.out.println("主人扔了一只鱼");
        c.eat();
    }

    public void feed(Bird b) {
        System.out.println("主人扔了不知道什么东西");
        b.eat();
    }*/
    // Master 直接面对他们的父类,耦合度高
    public void feed(Animal a){
        System.out.println("主人扔了不知道什么东西");
        a.eat();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值