Compare commits
No commits in common. "a86d75705344a9c9a375be52d00d3abf226d9ca9" and "cf6183c5b845816afd360d9949d3d76ff99dd019" have entirely different histories.
a86d757053
...
cf6183c5b8
@ -1,14 +1,4 @@
|
|||||||
package chapter4;
|
package chapter4;
|
||||||
|
|
||||||
public class Example25 {
|
public class Examaple25 {
|
||||||
public static void main(String[] args) {
|
|
||||||
int result = divide(4, 0);
|
|
||||||
System.out.println("结果:" + result);
|
|
||||||
|
|
||||||
System.out.println("程序继续运行...");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int divide(int x, int y) {
|
|
||||||
return x / y;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,36 +1,2 @@
|
|||||||
package chapter4.example14;
|
package chapter4.example14;public class Example14 {
|
||||||
|
|
||||||
abstract class Animal {
|
|
||||||
abstract void shout();
|
|
||||||
}
|
|
||||||
|
|
||||||
class Cat extends Animal {
|
|
||||||
public void shout() {
|
|
||||||
System.out.println("喵喵...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Dog extends Animal {
|
|
||||||
public void shout() {
|
|
||||||
System.out.println("汪汪...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Example14 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Animal cat = new Cat();
|
|
||||||
Animal dog = new Dog();
|
|
||||||
|
|
||||||
cat.shout();
|
|
||||||
dog.shout();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Example15 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Dog dog = new Dog();
|
|
||||||
Animal an = dog;
|
|
||||||
|
|
||||||
an.shout();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package chapter4.object;
|
package chapter4.object;
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
class Student {
|
class Student {
|
||||||
private String name;
|
private String name;
|
||||||
private int age;
|
private int age;
|
||||||
@ -11,17 +9,11 @@ class Student {
|
|||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public String toString() {
|
public String toString() {
|
||||||
// return "Student {name=" + name + ", age=" + age + "}";
|
return "Student {name=" + name + ", age=" + age + "}";
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Test {
|
public class Test {
|
||||||
public static void main(String[] args) {
|
|
||||||
Student s1 = new Student("张三", 23);
|
|
||||||
|
|
||||||
System.out.println(s1);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,21 +0,0 @@
|
|||||||
package chapter5;
|
|
||||||
|
|
||||||
public class Example01 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// 1、创建一个空的字符串
|
|
||||||
String str1 = new String();
|
|
||||||
// 2、创建一个内容为abcd的字符串
|
|
||||||
String str2 = new String("abcd");
|
|
||||||
// 3、创建一个内容为字符数组的字符串
|
|
||||||
char[] charArray = new char[]{'D', 'E', 'F'};
|
|
||||||
String str3 = new String(charArray);
|
|
||||||
// 4、创建一个内容为字节数组的字符串
|
|
||||||
byte[] arr = {97, 98, 99};
|
|
||||||
String str4 = new String(arr);
|
|
||||||
|
|
||||||
System.out.println("a" + str1 + "b");
|
|
||||||
System.out.println(str2);
|
|
||||||
System.out.println(str3);
|
|
||||||
System.out.println(str4);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package chapter5;
|
|
||||||
|
|
||||||
public class Example02 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String s = "abcdabc";
|
|
||||||
System.out.println("字符串的长度为:" + s.length());
|
|
||||||
System.out.println("字符串中第一个字符:" + s.charAt(0));
|
|
||||||
System.out.println("字符c第一次出现的位置:" + s.indexOf('c'));
|
|
||||||
System.out.println("字符c最后一次出现的位置:" + s.lastIndexOf('c'));
|
|
||||||
System.out.println("子字符串ab第一次出现的位置:" + s.indexOf("ab"));
|
|
||||||
System.out.println("子字符串ab字符串最后一次出现的位置:" + s.lastIndexOf("ab"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package chapter5;
|
|
||||||
|
|
||||||
public class Example03 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String str = "abcdEFG";
|
|
||||||
|
|
||||||
// 1、转换成字符数组
|
|
||||||
System.out.print("将字符串转为字符数组后的结果:");
|
|
||||||
char[] charArray = str.toCharArray();
|
|
||||||
for (char c : charArray) {
|
|
||||||
System.out.print(c); // 打印当前遍历到的字符
|
|
||||||
System.out.print(','); // 每打印一个字符后,加一个逗号
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2、把数字转换成字符串(好用)
|
|
||||||
System.out.println();
|
|
||||||
System.out.print("把数字123转换成字符串的效果:");
|
|
||||||
System.out.println(String.valueOf(123));
|
|
||||||
|
|
||||||
// 3、大小写转换(常用)
|
|
||||||
System.out.println("转换成小写:" + str.toLowerCase());
|
|
||||||
System.out.println("转换成大写:" + str.toUpperCase());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package chapter5;
|
|
||||||
|
|
||||||
public class Example04 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String name = "王小明";
|
|
||||||
String newName = name.replace("王", "卢");
|
|
||||||
System.out.println("旧名字:" + name + "\n新名字:" + newName);
|
|
||||||
|
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
String name2 = " 王 小明 ";
|
|
||||||
String name3 = name2.trim();
|
|
||||||
System.out.println("去除空格前:" + name2 + "\n去除空格后:" + name3 );
|
|
||||||
// 思考:如何去掉所有空格?
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package chapter5;
|
|
||||||
|
|
||||||
public class Example05 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String s1 = "我是王小明";
|
|
||||||
String s2 = "我是";
|
|
||||||
|
|
||||||
System.out.println("判断开头:" + s1.startsWith("我是"));
|
|
||||||
System.out.println("判断结尾:" + s1.endsWith("小明"));
|
|
||||||
System.out.println("判断包含:" + s1.contains("是"));
|
|
||||||
System.out.println("判空:" + s1.isEmpty());
|
|
||||||
System.out.println("判断相等:" + s1.equals(s2));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package chapter5;
|
|
||||||
|
|
||||||
public class Example06 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String str = "石家庄-武汉-哈尔滨";
|
|
||||||
// 1、截取
|
|
||||||
System.out.println("从第5个字符截取到末尾:" + str.substring(4));
|
|
||||||
System.out.println("从第5个字符截取到第6个字符:" + str.substring(4, 6));
|
|
||||||
// 2、分割
|
|
||||||
System.out.println("分割后字符串数组中的元素依次是:");
|
|
||||||
String[] array = str.split("-");
|
|
||||||
for (String s : array) {
|
|
||||||
System.out.println(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user