1.static(静态)
1.1静态变量
static 数据类型 变量名字;
例
package com.qfedu.a_static;
class Snake{
String name;
int age;
static double weight;
}
public class Demo1 {
public static void main(String[] args) {
//类创建时就存在方法区,静态方法创建时就存入方法区
Snake.weight = 66;//对静态方法赋值,用类,
System.out.println(Snake.weight);
//创建对象时变量才存入栈区和堆区,//用对象的引用
Snake snake = new Snake();
snake.name = "小青";
System.out.println(snake.name);
}
}
1.2java中的静态
也就是指公共的,所有人都可以用,但是并不是属于某个人的。
1.2.1使用static修饰成员变量
公共的属性,与对象无关,由类调用
package com.qfedu.a_static;
class Snake{
String name;
int age;
static double weight;
}
public class Demo1 {
public static void main(String[] args) {
//类创建时就存在方法区,静态方法创建时就存入方法区
Snake.weight = 66;//对静态方法赋值,用类,
System.out.println(Snake.weight);
//创建对象时变量才存入栈区和堆区,//用对象的引用
Snake snake = new Snake();
snake.name = "小青";
System.out.println(snake.name);
}
}
1.2.2使用static修饰成员方法
修饰的方法由类调用,是储存在方法内存中
package com.qfedu.a_static;
class Person{
public void eat() {
System.out.println("吃东西");
}
//使用static修饰的成员方法
public static void swim() {
System.out.println("去游泳");
}
}
public class Demo2 {
public static void main(String[] args) {
//不需要创建对象就可以使用,与对象无关
Person.swim();//去游泳
Person person = new Person();
//成员方法与对象有关,需要创建对象后才可以调用
person.eat();//吃东西
//堆区和方法区是相通的,任意一个区方法被创建,两个区的值都被同步
person.swim();//The static method swim() from the type Person should be accessed in a static way
}
}
1.2.3静态代码块
在类中写出来就可以用
优先级:静态代码块》构造代码块》构造方法
package com.qfedu.a_static;
class People{
public People() {
System.out.println("构造方法");
}
{
System.out.println("构造代码块");
}
static {
System.out.println("静态构造代码块");
}
}
public class Demo3 {
public static void main(String[] args) {
People people = new People();
}
}

2.异常
2.1生活中的异常
人生病,要去医院,找医生治病,在java中,程序员相当于医生
2.2java中的异常
程序在运行时,难免会发生一些不可避免的程序错误,也就是编译错误和运行错误。
比如数组下标越界异常,运算条件错误异常,等等。
异常类有两个主要的子类:IOException 类和 RuntimeException 类。
Java 异常处理
检查性异常: 最具代表的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单地忽略。
运行时异常: 运行时异常是可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时被忽略。
错误: 错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译也检查不到的。
异常方法
下面的列表是 Throwable 类的主要方法:
序号 方法及说明
1 public String getMessage()
返回关于发生的异常的详细信息。这个消息在Throwable 类的构造函数中初始化了。
2 public Throwable getCause()
返回一个 Throwable 对象代表异常原因。
3 public String toString()
返回此 Throwable 的简短描述。
4 public void printStackTrace()
将此 Throwable 及其回溯打印到标准错误流。。
5 public StackTraceElement [] getStackTrace()
返回一个包含堆栈层次的数组。下标为0的元素代表栈顶,最后一个元素代表方法调用堆栈的栈底。
6 public Throwable fillInStackTrace()
用当前的调用栈层次填充Throwable 对象栈层次,添加到栈层次任何先前信息中。
2.3.异常的解决方式
1.异常的捕捉
理解:获取异常,且不影响后续代码执行
格式
try {
有可能出现异常的代码
} catch (异常对象) {
//如何处理这个异常
}
2.异常的抛出
代码发生异常就会抛出异常代码,并在抛出点终止代码运行
两个抛出关键字:throw和throws
throw: 在方法中抛出一个异常。自己造一个错
throws: 在方法的声明处书写,告知当前调用者,这个地方有问题。
package com.qfedu.a_static;
public class Demo4 {
public static void main(String[] args) throws Exception {
int[] arr = new int[3];
test(arr);
}
public static void test (int[] arr) throws Exception {
if (arr[3] == 5) {//编译异常Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
throw new Exception();
}
System.out.println("哈哈");
}
}
#### 2.3Throwable类
构造方法:
>
1.`Throwable()`构造一个新的可抛出的 `null`作为其详细信息。 |
2.Throwable(String message)`构造一个具有指定的详细消息的新的throwable。 |
> 方法:
1. `String` | `getMessage()`返回此throwable的详细消息字符串。 |
2. `void` | `printStackTrace()`将此throwable和其追溯打印到标准错误流。 |
3.`String` | `toString()`返回此可抛出的简短描述。 |
package com.qfedu.a_static;
public class Demo5 {
public static void main(String[] args) {
System.out.println("一只小猪");
System.err.println("两只小猪");
Throwable throwable = new Throwable();
throwable.toString();//会返回异常的最简值,为红色的,且在规范输出的上面
//int[] arr = new int[2];
//arr[4] = 55;
//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
//at com.qfedu.a_static.Demo5.main(Demo5.java:10)
//throwable.getMessage();
Throwable throwable2 = new Throwable("狗蛋");
System.out.println(throwable2.toString());
throwable2.printStackTrace();
//java.lang.Throwable: 狗蛋
//at com.qfedu.a_static.Demo5.main(Demo5.java:14)
System.out.println(throwable2.getMessage());
}
}
自定义异常
子类需要的异常有时父类无法满足,此时就需要子类自己制造异常
public class MyException extends Exception {
public MyException() {
super();
}
public MyException(String str) {
super(str);
}
}
public class Test07 {
public static void main(String[] args) {
int age;
Scanner input = new Scanner(System.in);
System.out.println("请输入您的年龄:");
try {
age = input.nextInt(); // 获取年龄
if(age < 0) {
throw new MyException("您输入的年龄为负数!输入有误!");
} else if(age > 100) {
throw new MyException("您输入的年龄大于100!输入有误!");
} else {
System.out.println("您的年龄为:"+age);
}
} catch(InputMismatchException e1) {
System.out.println("输入的年龄不是数字!");
} catch(MyException e2) {
System.out.println(e2.getMessage());
}
}
}
finally关键字
finally 关键字用来创建在 try 代码块后面执行的代码块。
无论是否发生异常,finally 代码块中的代码总会被执行。
在 finally 代码块中,可以运行清理类型等收尾善后性质的语句。
finally 代码块出现在 catch 代码块最后,语法如下:
try{
// 程序代码
}catch(异常类型1 异常的变量名1){
// 程序代码
}catch(异常类型2 异常的变量名2){
// 程序代码
}finally{
// 程序代码
}
3.String类
String的两种声明方式
1.String str = “狗蛋”;
2.String str1 = new String(“狗蛋”);
开发中字符串的比较运用的是equals.
str与str1不在同一个地址中

249

被折叠的 条评论
为什么被折叠?



