java:课后笔记wk45

1. class

1.1 toString()和equals()

public class People{
	private int age;
	private String name;
	
	public People(int age, String name){
		this.age = age;
		this.name = name;
	}

	public String toString(){
		return name + " " + age;
	}

	public boolean equals(People anotherPeople){
		return anotherPeople.age == this.age && anotherPeople.name = this.name;
	}
}


public class Main{
	pblic static void main(String[] args){
		People p = new People(25,"Skylar");
		System.out.println(p);
		//会输出Skylar 25

		People p2 = new People(25, "Skylar");
		System.out.println(p.equals(p2));

	}
}

1.2 overload-constructor

  1. default
  • 没有其他任何constructor的情况下会自动补充。【建议都手动写
  • 有其他constructor fun就不会自动补充,会报错
public class People{
	private int age;
	private String name;
	
	//default constructor
	public People(){
	}
}

public class Main{
	pblic static void main(String[] args){
		People p = new People();
		System.out.println(p);
		//会输出 null 0【默认值】

	}
}
  1. 普通的
  • 传入的variables来自instance variables。
  1. copy- constructor
  • 把一个object的值,copy给另一个object。
public class People{
	private int age;
	private String name;
	
	// copy-constructor
	//将anotherPeople的信息copy给这个people
	public People(People anotherPeople){
		this.age = anotherPeople.age;
		this.name = anotherPeople.name;
	}
}

public class Main{
	pblic static void main(String[] args){
		People p = new People(25, "Skylar");
		People p2 = p;//把p的address给到p2,所以p,p2会同时改变

		//使用copy constructor
		//将p的值,拷贝给p3。
		People p3 = new People(p);
	}
}

1.3 static

  • 当一个东西,属于整个class,就是static
  • static method和static variable
  1. static variable:初始化过程跟instance variable一样。
  2. static method:建议调用时使用class name。
public class People{
	private int age;
	private String name;
	private static int number = 0;
	
	public People(int age, String name){
		this.age = age;
		this.name = name;
		number++;
	}
	public People(People anotherPeople){
		this.age = anotherPeople.age;
		this.name = anotherPeople.name;
		number++;
		
	}
	public People(){
		number++;
	}

	public String toString(){
		return name + " " + age;
	}

	public boolean equals(People anotherPeople){
		return anotherPeople.age == this.age && anotherPeople.name = this.name;
	}

	public static void printClassInfo(){
		System.out.println("Current number: " + number);
	}
}

public class Main{
	pblic static void main(String[] args){
		People p = new People(25,"Skylar");
		//调用时用class name调用【用object也可以但不建议】
		People.printClassInfo();
		System.out.println(p);
		//会输出Skylar 25

		People p2 = new People(25, "Skylar");
		People.printClassInfo();
		System.out.println(p.equals(p2));
	}
}

2. Wrapper

  1. 包装类:为什么?怎么样?
  2. class的使用很灵活,基础数据类型不灵活
  3. boxing:将基础数据类型 = > wrapper class 的过程。
  4. unboxing: wrapper class => 基础数据类型 的过程。
    【byte, short, int, long, float, double, char, boolean】
    【Byte, Short, Integer, Float, Double, Character, Boolean】
  5. 注意⚠️:当使用Boolean时,要认为手动的去判断用户输入的是flase还是其他信息。然后再判断true/false。
  6. Character不能使用parse。【String是character的集合啦】
  7. substring(起, 终),不包含终。
  8. String => char
  9. toUpperCase():转换成大写
  10. isUpperCase():回答是否为大写
  11. toLowerCase():转换成小写
  12. isLowerCase(): 回答是否为小写
  13. isWhitespace():回答是否有空格【3种:普通空格、\t 、\n】
  14. isLetter(): 回答是否是字母
  15. isDigit():回答是否是数字
  16. isLetterOrDigit():回答是否是字母 / 数字
public class Main{
	public static void main(String[] args){
		Integer.parseInt("10");
		//特别注意Boolean
		//只要不是“True”,全部返回False【不考虑大小写】
		System.out.println(Boolean.parseBoolean("True"));//true
		System.out.println(Boolean.parseBoolean("TRue"));//true
		System.out.println(Boolean.parseBoolean("False"));//false
		System.out.println(Boolean.parseBoolean("sfgdfg"));//false

		//String => char
		String s = "helloworld!";
		char x = s.charAt(5);/获取index有5时的内容。

		//应用:检查id和密码是否合规
		Scanner sc = new Scanner(System.in);
		String id = sc.nextLine();
		String password = sc.nextLine();

		boolean allNum = true;
		for(int i = 0; i < id.length; i++){
			//看id里每一位是否为数字
			if(!Character.isDigit(id.charAt(i))){
				allNum = false;
			}
		}
		String results = allNum ? "id合法": "id非法";
		System.out.println(results);

		boolean hasNum = false;
		boolean hasUpper = false;
		boolean hasLower = false;
		for(int i = 0; i < password.length; i++){
			if(Character.isUpperCase(password.charAt(i))){
				hasUpper = true;
			}else if(Character.isLowerCase(password.charAt(i))){
				hasLower = true;
			}else if(Character.isDigit(password.charAt(i))){
				hasNum = true;
			}
		}
		String results = hasNum && hasUpper && hasLower ? "密码合法": "密码不合法";
	}
}

3. Maths

  • java.lang: String, Wrapper, Math
  • 绝大多数都是static
  1. abs、max、min
  2. pow(底数,指数)
  3. ceil、floor、sqrt

4. array

  1. array拷贝思路
    1. 创建一个新的array
    2. copy旧array的内容
    3. 再将想添加的内容填入新的array
    4. 最后,将新array的地址赋给旧的人、array。
  2. 数组的另一种for loop请添加图片描述
public class Main{
	public static void main(String[] args){
		People[] peoples = new People[10];
		//将存放类型为People这个class,名字叫peoples的数组,打印里面的每个数据。
		for(People p: peoples){
			System.out.println(p);
		}
	}
}	

5. arrayList

  • 数组的局限性:长度固定。
    =>. 想要一个像数组一样,能把相同数据类型放一起的,但长度可以自动修改的。=> array list.
  1. arraylist排序非常方便!
  2. arraylist会自动翻倍/缩放。【当位子不够了,原本4位,它会自动扩展到8位。删除同理缩放一倍】
  3. 缺点:访问速度比array慢一点。arraylist里的数据类型只能是class,无法存储基础数据类型
import java.util.ArrayList;

public class Main{
	public static void main(String[] args){
		//【公式】
		ArrayList<baseType> watches = new ArrayList<baseType>(初识容量大小);

		//举例
		ArrayList<String> watches = new ArrayList<>();
		//增加内容
		watches.add("AP");
		watches.add("PP");
		watches.add("Constantin");
		System.out.println(watches);//输出[AP, PP, Constantin]

		//访问
		System.out.println(watches.get(0));//输出AP

		//修改arraylist内容
		watches.set(0, "Omega");//将第一位改成Omega[Omega,PP,Constantin]

		//删除arraylist某一个内容
		watches.remove(2);//将index=2的那一位直接删除。[Omega,PP]
	
		//获得watches的长度
		watches.size();//输出2

		//排序
		Collections.sort(watches);//按照首字母进行排列。
	}
}
  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值