java入门
基本环境:jdk(环境变量) IDEAC
1、编译 运行
public class test { public static void main(String[] args) { System.out.println("hello world\n"); } }
javac test.java(编译) java test(执行)
2、变量
用法:int year = 1024;
8个基本类型:byte、short、int、long char(16位) boolean float、double
常量字面值:24、24L、24f、24.0F、1.2e3、 ox1a、071、'c'、"string"
类型转换:小转大没问题、大转小会有损失
变量命名:字母 数字 _$,首字母不能是数字
常量final:不可再赋值,final int year = 1024
3、操作符
算术操作符:+ - * / %, ++ --
关系操作符:>、>=、<、<=、==、!=
逻辑运算符:&&、||、!
位运算符:&(位与)、|(位或)、^(位异或)、~(位反)、<<(左移)、>>(右移)
赋值运算符:+=、*=、%=、|=、<<=
三元运算符: ?:
4、控制流程
if、while、for、switch、continue、break
if( i < 5){ System.out.print(i); } switch(var){ case 1: expresstion1; break; default: expression2; } while(i < 5){ i++; } for(int i = 0; i < 5; i++){ System.out.print(i) }
5、数组
声明、创建: int[] a; a = new int[5]; a.length
初始化: int[] a = new int[5]; int[] a = new int[]{1,2,3,6,4}; int[] a = {1,2,3,6,4};
遍历:for(int each: a)
复制:System.arraycopy(src, srcPos, dest, destPos, length)
6、类对象
类的创建 对象的引用:
public class Hero extend Person { String name; float height = 178.0f; int age; public Hero() { System.out.println("实例化一个对象的时候,必然调用构造方法"); } public Hero(String name){ this.name = name; } public static void main(String[] args) { new Hero(); Hero h = new Hero(); } }
继承父类的属性和函数,想想子父有相同的函数 那调用谁的。
函数方法重载
public void attack() public void attack(int a) public void attack(int a, float b)
无参构造方法、有参构造方法、构造方法重载
传递参数:基本类型、数组 对象。。。指针的应用
包package:把比较接近的类规划在同一个包下
成员变量/函数的修饰符:private、pubilc、protected

static:类属性/方法 静态属性/方法 所有对象共享。。。不需要创建对象就能访问的静态方法。
7、接口与继承
什么是接口interface(好像可有可无),接口提供的不只是必须被Override的抽象方法,也可以是默认方法default public void attack(){……};
public interface AD { public void physicAttack(); } public interface AP { public void magicAttack(); } public class Hero extends Person implements AD,AP{ @Override public void magicAttack() { System.out.println("进行魔法攻击"); } @Override public void physicAttack() { System.out.println("进行物理攻击"); } }
类型转换:子父类之间才可以转;子类转父类没问题Person p = new Hero(),父类强制转之类可能有问题Hero h = Hero(new Person())
多态两条件:①假设子父两类都实现了attack函数 ②父引用指向子对象Person p = new Hero();p.attack调用的将是Hero.attack()函数。
多态来自继承。
Object继承类:Object是所有类的父类,有toString()、finalize()、equals()、hashCode()
boolean equals(Object o){return this.age == o.age;},区别于==;
hashCode()的作用在于字典HashMap能以O(1)的时间找到key。因为底层是一个数组,下标为每一个key对应的hashCode值(但有可能hashCode重复哦)。
public final class Hero extends Person{……}:表示Hero不能被继承
抽象类:不可被实例化,可以没有抽象方法。 public abstract class Hero{int age; public abstract void attack();}
抽象类与接口:①子类只能继承一个抽象类,但可以继承多个接口 ②接口的属性必须是public/static/final。
内部类:
public class Hero { private String name; // 姓名 class BattleScore { int kill; BattleScore(int kill){ this.kill = kill; } public void legendary() { if (kill >= 8) System.out.println(name + "超神!"); else System.out.println(name + "尚未超神!"); } } public static void main(String[] args) { Hero garen = new Hero(); garen.name = "盖伦"; BattleScore score = garen.new BattleScore(9); score.legendary(); } }
public abstract class Hero { String name; //姓名 public abstract void attack(); public static void main(String[] args) { class SomeHero extends Hero{ public void attack() { System.out.println( name+ " 攻击..."); } } SomeHero h =new SomeHero(); h.name ="天空龙"; h.attack(); } }
8、数字封装类
基本数据类型对应的装箱类Integer、Byte、Short、Long、Float、Double、Boolean 、Character
装箱/拆箱:Integer it = new Integer(i); int i = it.intValue();
9、格式化输出
System.out.println("hello " + 100 + 24); 输出hello 10024
System.out.printf("hello %s, my age is %d", "hello", 24);
2022-03-17 12:01:53

浙公网安备 33010602011771号