2.面向对象
程序员文章站
2022-07-20 10:33:48
1.1.类的定义 创建类 创建对象 1.2.private关键字 1.3.构造方法 1.4.Scanner类 1.5.Random类 1.6.ArrayList集合 1.7.String类 ......
1.1.类的定义
创建类
package derek.day06.demo; public class student { //成员变量 string name; int age; //成员方法 public void eat(){ system.out.println("吃饭"); } public void study(){ system.out.println("学习"); } }
创建对象
package derek.day06.demo; public class demostudent { public static void main(string[] args) { //创建对象 student stu = new student(); stu.name = "derek"; stu.age = 18; system.out.println(stu.name); stu.eat(); } }
1.2.private关键字
package derek.day06.demo; public class student { //成员变量 string name; private int age; public void show(){ system.out.println(name+":"+age); } public void setage(int num){ if (num < 100 && num > 0){ age = num; }else { system.out.println("错误的数据"); } } public int getage(){ return age; } }
package derek.day06.demo; public class demostudent { public static void main(string[] args) { student stu = new student(); stu.name = "derek"; stu.setage(18); stu.show(); } }
1.3.构造方法
package derek.day06.demo; import org.w3c.dom.ls.lsoutput; public class student { //成员变量 private string name; private int age; //无参数的构造方法 public student(){ system.out.println("无参数的构造方法执行"); } //全参数的构造方法 public student(string name, int age){ system.out.println("全参构造方法执行"); this.name = name; this.age = age; } public void setname(string name){ this.name = name; } public string getname(){ return name; } public void setage(int age){ this.age = age; } public int getage(){ return age; } }
package derek.day06.demo; public class demostudent { public static void main(string[] args) { student s1 = new student(); //无参数的构造方法执行 student s2 = new student("derek", 18); //全参构造方法执行 } }
1.4.scanner类
package derek.learn; //1.导包 import java.util.scanner; public class demoscanner { public static void main(string[] args) { //2.创建,system.in代表从键盘进行输入 scanner sc = new scanner(system.in); //3.使用,获取键盘输入的int数字 int num = sc.nextint(); system.out.println(num); //字符串 string name = sc.next(); system.out.println(name); } }
1.5.random类
package derek.learn; import java.util.random; public class demorandom { public static void main(string[] args) { random r = new random(); int num = r.nextint(10); system.out.println(num); } }
1.6.arraylist集合
package derek.learn; import java.util.arraylist; public class demoarraylist { public static void main(string[] args) { //创建一个arraylist集合,里面是string类型的数据 arraylist<string> list = new arraylist<>(); system.out.println(list); //[] //1.向集合中添加数据 list.add("迪丽热巴"); list.add("古力娜扎"); list.add("马尔扎哈"); system.out.println(list); //[迪丽热巴, 古力娜扎, 马尔扎哈] //2.获取 system.out.println(list.get(1)); //古力娜扎 //3.删除 list.remove(1); system.out.println(list); //[迪丽热巴, 马尔扎哈] //4.获取集合长度 system.out.println(list.size()); //2 //5.遍历 for (int i = 0; i < list.size(); i++) { system.out.println(list.get(i)); } } }
1.7.string类
package derek.learn; public class demostring { public static void main(string[] args) { string str1 = "zhang"; string str2 = "derek"; //1.拼接字符串 string str3 = str1.concat(str2); system.out.println(str3); //zhangderek //2.获取指定索引位置的字符 system.out.println(str1.charat(1)); //h //3.获取子字符串的索引 system.out.println(str3.indexof('e')); //6 //4.截取 system.out.println(str3.substring(1,4)); //han //5.转换成为字符数组 char[] chars = "hello".tochararray(); system.out.println(chars[1]); //e //6.替换 string str4 = "aaabbbccc"; string str5 = str4.replace('a','d'); system.out.println(str5); //dddbbbccc //7.分割 string str6 = "aa,bb,cc,d"; string[] arr = str6.split(","); system.out.println(arr[1]); //bb } }
上一篇: 这是看到真爱了
下一篇: JavaScript仿微信打飞机游戏
推荐阅读