您的位置:首页 > 编程语言 > Java开发

JAVA学习第七天

2015-07-22 18:50 465 查看

目录

equals toString方法的重写

自定义异常

随便输入一个等式包含加减乘除

栈先进后出

递归:方法调用本身方法

String s=null; System.out.print(s+”aa”); 输出结果nullaa

Object祖类 java所有的引用数据全部继承于Object

equals 比较的是两个引用对象是否相同

equals toString方法的重写

package com.lingzhuo.test;

public class Student {
private  String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private   int age;
private int id;

@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(obj!=null&&obj  instanceof Student){// instanceof判断obj是否是Student的对象
return  ((Student) obj).getId()==this.id;//this指的是Student
}
return false;
}

@Override
public String toString() {//返回该对象的字符串表示。
// TODO Auto-generated method stub
return this.name;
}
//    @Override
//    public String toString() {
//      // TODO Auto-generated method stub
//      return super.toString();
//    }
}


Test

package com.lingzhuo.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Student1 {

public void readBook() throws FileNotFoundException,IOException{//throws方法抛出异常
FileInputStream is=new FileInputStream("d:11.txt");
is.read();
}

}


自定义异常

MyException1

package com.lingzhuo.test;

public class MyException1 {
private  String name;

public String getName() {
return name;
}

public void setName(String name) throws MyException   {
//如果MyException中 extends  RuntimeException    throws MyException可以省略
if(name.equals("张三")){
throw   new MyException();//new  还有括号()  不可以少了
}
this.name = name;
}
}


MyException

//自定义异常
package com.lingzhuo.test;

public class MyException extends  Exception{
//public class MyException extends  RuntimeException{
@Override
public void printStackTrace() {
// TODO Auto-generated method stub
System.err.println("不准叫张三的当老师");
//err打印出的异常为红色的  out打印出来的不是红色的
super.printStackTrace();
}

}


MyExceptionTest

package com.lingzhuo.test;

public class MyExceptionTest {
//  public static void main(String[] args) throws MyException {
public static void main(String[] args){
MyException1   teacher=new MyException1();
try {
teacher.setName("张三");
} catch (MyException e) {
// TODO Auto-generated catch block

e.printStackTrace();
System.out.println("捕获到异常");
}

System.out.println("程序完成");
}

}


copy



throws关键字

使用throws关键字声明方法可能要抛出的各种异常,throws只能出现在方法头。

如:

public void 方法名() throws 异常类型


throw关键字

throw关键字用于自定义异常,然后手动抛出。throw关键字只能出现在方法体内。

如:

public void 方法名(){
throw new 自定义异常名();
}


随便输入一个等式包含加减乘除

//随便输入一个等式包含加减乘除   计算结果
package com.donghe.test;

import java.util.Scanner;

public class Add2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MyStackNum2 num = new MyStackNum2();
MyStackChar2 fuhao = new MyStackChar2();
// 5+31*2
//      String s = "5+31*2/3-6-7*5+12=";//
String s=scanner.next();
String digit = "";// 要放在外面放在循环里面的话每次都会被置空 会发生NumberFormatException
for (int i = 0; i < s.length(); i++) {

char c = s.charAt(i);

if (Character.isDigit(c)) {
digit += c;// 5 31

} else {
// 加号
// 乘号
num.push(Integer.parseInt(digit));// 5入栈 31入栈 2入
if (c == '=') {
char top4=fuhao.pop();
int after = num.pop();
int before = num.pop();

int result = yunsuan(before, top4, after);

if(fuhao.getIndex()==0){
char top3=fuhao.pop();
int before2=num.pop();
result=yunsuan(before2,top3,result);

}

System.out.println(result);
} else {

digit = "";

if (fuhao.getIndex() > -1) {
jisuan(num, fuhao, c);
} else if (fuhao.getIndex() == -1) {
fuhao.push(c);// 加号入栈 Index=1;

}
}

}

}

}

private static void jisuan(MyStackNum2 num, MyStackChar2 fuhao, char c) {

char top = fuhao.getPop();// 加号 乘号入栈 index为2 //除号 得到加号

if (youxianji(c, top)) {
fuhao.push(c);
// 除号运行过来
} else {

top = fuhao.pop();// *出栈 /出栈
int after = num.pop();
int before = num.pop();
int result = yunsuan(before, top, after);
num.push(result);//
fuhao.push(c);// 减号放入

if ((c == '+' || c == '-') && (fuhao.getPop1() == '+' || fuhao.getPop1() == '-')) {
if (num.getIndex() == 1) {

char top1 = fuhao.pop1();
int after2 = num.pop();
int before2 = num.pop();
int result2 = yunsuan(before2, top1, after2);
num.push(result2);//
fuhao.push(c);

} else {
return;
}
///////

}

// jisuan(num, fuhao, c);//c为减号 before=5 after=20
}
}

public static int yunsuan(int before, char c, int after) {
int i = 0;
switch (c) {
case '+':
i = before + after;
break;
case '-':
i = before - after;
break;
case '/':
i = before / after;
break;
case '*':
i = before * after;
break;
default:
break;
}
return i;
}

public static boolean youxianji(char c, char top) {// c优先级高为true
if (c == '*' || c == '/') {
if (top == '*' || top == '/') {
return false;

}
return true;
}

return false;

}

}


package com.donghe.test;

public class MyStackChar2 {
private  char[]  array=new char[20];
private  int index=-1;////////

public char   getPop(){//得到一个符号
return array[index];
}

public int getIndex() {
return index;

}
public void push(char i){//入栈
index++;//写错了
array[index]=i;

}

public char   pop(){//
char  j=array[index];
index--;//出栈
return  j;
}
public char   pop1(){//出栈

char  j=array[0];
index=-1;
return  j;

}

public char getPop1() {
// TODO Auto-generated method stub
return array[0];

}
}


package com.donghe.test;

public class MyStackNum2 {
private  int[]  array=new int[20];
private  int index=-1;//////

public int getIndex() {
return index;
}

public void push(int i){//入栈
index++;
array[index]=i;

}
public int   pop(){
int  j=array[index];
index--;
return  j;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: