Java学习笔记_2020_12_03

类和对象

  • 类是我们自己创建的一种引用数据类型

例如:

// 引用数据类型
public class Student {
	/*
	 * 类中成员属性(创建对象时自动初始化):
	 * 	我们属性和数组一样会自动初始化。
	 * 		数值型:0
	 * 		布尔:false
	 * 		char:'\0'
	 * 		引用:null
	 * */ 
	int id;
	String name;
	String gender;
	int age;
	//类中成员方法(也称行为):
	public void eat() {
		System.out.println(name + "正在吃东西...");
	}
	
	public void sleep() {
		System.out.println(name + "正在吃睡觉...");
	}
}

//实例化对象:就是用抽象出来的类创建具体的对象,并为对象属性赋值
public class Test {

	public static void main(String[] args) {
		// 语法:类名 对象名 = new 类名();
		/*
		 * 对象的使用:
		 * 	访问属性:对象名.属性名
		 * 	访问行为:对象名.行为名()
		 * */ 
		Student stu = new Student();
		stu.id = 1;
		stu.name = "张三";
		stu.age = 22;
		stu.gender = "女";
		
		System.out.println(stu.name);
		stu.eat();
		stu.sleep();
		
		Student stu1 = new Student();
		stu1.id = 2;
		stu1.name = "诸葛亮";
		stu1.age = 21;
		stu1.gender = "男";
		
		Student stu2 = stu;
		stu2.id = 3;
		stu2.name = "黄月英";
		stu2.age = 21;
		stu2.gender = "女";
		
		System.out.println(stu.name);
		System.out.println(stu1.name);
		System.out.println(stu2.name);
	}

}

构造方法

构造方法

  • 构造方法没有返回值。包括没有void
  • 构造方法的名称必须和类名保持一致

每个类必须要有构造方法:

  • 如果我们在写程序的过程中没有手动声明构造方法,则系统会帮我们自动添加一个无参数无内容的构造方法。
  • 构造方法在我们创建对象时自动调用,而且只会调用一次。
  • 构造方法和我们普通方法一样,支持重载。

构造方法的作用:

  • 用于创建对象
  • 用于初始化对象中的属性
public class Student {
	int id;
	String name;
	int age;
	String sex;

	/*
	 * 	this: 当前类对象
	 * 		-this.属性 : 调用当前类对象中的属性
	 * 		-this.行为() :调用当前类对象中的行为
	 * 		-this() :调用当前类的构造方法,会根据参数自动识别调用哪一个。this()必须写在构造方法的第一行。
	 */
	public Student(int id, String name, int age) {
		this(id, name);
		this.age = age;
		this.sex = "男";
	}
	
	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	public Student() {

	}

	public void eat() {
		System.out.println(this.name + "正在吃东西...");
	}
}

static关键字

用于修饰属性:

  • 被static修饰的属性称之为静态属性(或类属性)。静态属性会在虚拟机加载类对象时在静态常量区自动分配空间。
  • 静态属性的调用:类名.属性名

用于修饰方法:

  • 被static修饰的方法称之为静态方法。静态方法在虚拟机加载类对象时在静态常量区分配空间。
  • 静态方法的调用:类名.方法名()

用于修饰代码块:

  • 被static修饰的代码块称之为静态代码块。静态代码块在虚拟机加载类时执行,先于构造方法执行。
public class Student {
	int id;
	String name;
	int age;
	String sex;
	static int count;//用static修饰

	// 用static修饰的代码块:代码块在系统调用构造方法之前进行调用
	static {
		System.out.println("代码块");
	}

	public Student(int id, String name, int age, String sex, int count) {
		System.out.println("构造方法");
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
		Student.count = count;
	}

	public static void eat() {
		System.out.println(count + "正在吃东西...");
	}
}

Java常用类

equal:

  • 用于比较两个字符内容是否相等,返回值类型为Boolean
		//此种赋值在静态常量区申请空间存储字符串“Hello World”
		String str = "Hello World";
		//此种赋值在堆空间中存储字符串“hello world”
		String str1 = new String("hello world");
		//此上两种赋值方式,地址不同

		System.out.println(str);
		System.out.println(str1);
		// 如果 == 比较的是引用数据类型时,比较的地址
		System.out.println(str==str1);
		/*
		//语法:str.equal(str1) 
				or  
			str.equalIgnoreCase(str1)
		 * equals:比较字符串内容是否相当
		 */
		System.out.println(str.equalsIgnoreCase(str1));//忽视大小写
		System.out.println(str.equals(str1));//区分大小写

length:

  • 取得字符串的长度,返回值类型为int
String str="Hello World!";
System.out.println(str.length());

charAt:

  • 根据下标取得字符串中的字符,返回值类型为字符型
String str="Hello World!";
System.out.println(str.charAt(6));//返回W,下标从0开始

trim:

  • 去掉字符串前后的空格,返回值类型为string类型
String str=" Hello World! ";
System.out.println(str.trim());

endsWith/startsWit:

  • 判断字符串是否以所给字符串结尾或开头,返回值类型为Boolean类型
String str="Hello World!";
System.out.println(str.endsWith("ld!"));//判断str是否以ld!结尾
System.out.println(str.startsWith("Hel"));//判断str是否以Hel开头

indexOf/lastIndexof:

  • 判断字符串首次出现的位置/判断字符串最后一次出现的位置,返回值为int类型
String st="Hello World!";
System.out.println(str.indexOf("ll"));//判断字符串"ll"在str中首次出现的位置
System.out.println(str.lastIndexOf("ll"));//判断字符串“ll”在str中最后一次出现的位置

substring:

  • 字符串截取,返回值为string类型
String str="Hello World!";
System.out.println(str.substring(6));//从下标为6的字符截取到最后
Systen.out.println(str.substring(6,17));//从下标为6的字符截取到下标为16的字符。

replace:

  • 字符串替换,返回值为String类型
String str="Hello World!";
System.out.println(str.replace("Wo","abc"));//将str中的"Wo"替换成"abc"

compareTo:

  • 比较两个字符串的大小
    • 若字符串相等则返回0
    • 从前向后依次比较字符编码的差值。直到比较到不同的为止
    • 若字符串前面全部相同,就比较字符串的长度差值
String str="abc";
String str1="abcd2";
System.out.println(str.compareTo(str1));

toLowerCase:

  • 将字符串转换为小写,返回值类型String
String str="HELLO WORLD!";
System.out.println(str.toLowerCase());

toUpperCase:

  • 将字符串转换为大写,返回值为String类型
String str="hello world!";
System.out.println(str.toUpperCase());

StringBuffer类:

		//StringBuffer用来初始化一个字符串类型的数据
		StringBuffer sb = new StringBuffer(); // ""
		//在字符串sb后接一串字符串"Hello"
		sb.append("Hello");
		//使用append后接数据的类型可以是int boolean double float等
		sb.append(123);
		sb.append(true);
		/*
		使用StringBuffer类中的delete(start,end)方法可以删除从start起			至end间的字符,包括start位置但不包括end位置的字符
		*/
		sb.delete(8, sb.length());

		System.out.println(sb);

Math类及常用方法:

		//Math.E无理数e
		System.out.println(Math.E);
		//Math.PI圆周率π
		System.out.println(Math.PI);
		//Math.sin(角度);计算角度的正弦值;还有Math.cos()/tan()等
		System.out.println(Math.sin(Math.PI/3));
		// a的b次方
		System.out.println(Math.pow(4, 2));
		// 四舍五入
		System.out.println(Math.round(1.5));
		// 开平方根
		System.out.println(Math.sqrt(2));

Random类及常用方法:

		//使用Random类中方法时,需先实例化对象
		Random random = new Random();
		//随机一个Boolean型的量
		System.out.println(random.nextBoolean());
		//随机一个Int型的数
		System.out.println(random.nextInt());

//		for (int i = 0; i < 100; i++) {
//			System.out.println(random.nextInt(5) + 10);
//		}
		//随机一个Float型的数
		System.out.println(random.nextFloat());
		
		//System.currentTimeMillis()得到当前时间,以毫秒为单位 
		//得到当前的时间
		System.out.println(System.currentTimeMillis());
		
		// 强制关机。强制结束当前进程(程序)。
		System.exit(-1);
		//不常用的方法System.err.println("");
		System.err.println("错误");

Date类与simpleDateFormat类的搭配使用及常用方法:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

	public static void main(String[] args) throws ParseException {
        //使用Date中方法时需要先为Date创建对象
		Date date = new Date();
        
        //date.getTime()返回的是自1970年1月1日00:00:00 GMT以来的毫秒数
		System.out.println(date.getTime());
        
		//SimpleDateFormat类创建时需为其定义日期格式,
        //记住常见格式 yyyy/MM/dd hh:mm:ss
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        
		//将毫秒格式化为日期格式
		System.out.println(sdf.format(date));
		
		String str = "2020-12-03 14-48-07";
		
        //将日期格式转换为毫秒值
		Date d = sdf.parse(str);
		System.out.println(d.getTime());
		
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值