2019-7-18 [JavaSE] 阶段总结

it2022-05-05  165

文章目录

第一期1.while,do-while,for的区别;2.break,continue,return区别;3.数组初始化方式有几种?怎么实现?4.基本for和 增强for区别?5.代码:输出图形?6.代码:5个数求最大值?7.代码:冒泡排序? 第二期1.类和对象的区别?2.成员变量和局部变量的区别?3.什么是方法重载?4.this代表什么?作用是什么?5.定义三个重载方法max(),6.定义一个网络用户类: 第三期1.什么是方法重载?2.this代表什么?作用是什么?3.访问修饰符有几种?可见性范围?4.静态方法和实例方法区别?5.定义一个网络用户类:6.单例模式:饿汉式? 第四期1.什么是重写?2.final 和 finalize()的作用?3.抽象类 和 接口 的异同?4.多态实现定义一个游泳Swimming接口:5.输入一行字符串,编写代码实现取左串: 第五期1.String,StringBuffer,StringBuider区别?2.final,finalize,finally区别?3.throw和throws区别?4.写出所有的包装类名称?5.代码实现: 第六期1.List、Set作用?2.ArrayList 、Vector和 LinkedList区别?3.HashSet、LinkedHashSet、TreeSet区别?4.代码实现:TreeSet集合5.代码实现:读写文件

第一期

1.while,do-while,for的区别;

答:while,do-while时非固定循环 for是固定循环 while 先判断再循环 do-while先循环一次再判断

2.break,continue,return区别;

答:break:只能结束switch循环 continue :结束本次循环,进行下一次循环 return:结束方法

3.数组初始化方式有几种?怎么实现?

例如:给数组 int [] arr 赋值 11,22,33 答: 动态初始化: int arr = new int[3]; arr[0]=11; arr[1]=22; arr[2]=33; 静态初始化: int []arr = {11,22,33};

4.基本for和 增强for区别?

答: 基本for:遍历数组,前闭后开。可以逆序遍历,可以修改,可以遍历一部分。 增强for:遍历集合和数组。只能顺序遍历,不能修改,只能遍历所有。

5.代码:输出图形?

** **** ****** ********

答:

public class Demo{ public static void main(String[] args) { for(int i = 0; i < 4; i++) { for(int j = 0; j < 4-i; j++){ System.out.print(" "); } for(int k = 0; k < 2*i; k++){ System.out.print("*"); } System.out.println(); } } }

6.代码:5个数求最大值?

答:

public class Demo{ public static void main(String[] args) { int [] arr = {11,55,22,33,44}; int temp; for(int i = 0; i < arr.length-1; i++) { for(int j = 0; j < arr.length-1 - i; j++) { if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } int a = arr[4]; System.out.println(a); } }

7.代码:冒泡排序?

答:

public class Demo{ public static void main(String[] args) { int [] arr = {11,55,22,33,44}; int temp; for(int i = 0; i < arr.length-1; i++) { for(int j = 0; j < arr.length-1 - i; j++) { if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for(int a : arr) { System.out.println(a); } } }

第二期

1.类和对象的区别?

答:类是对象的抽象,对象是类的具体化 类是一个模板,可以创造对象,这些对象具有相同的特征和行为 类是引用数据类型,对象是对应是引用变量

2.成员变量和局部变量的区别?

答:1:作用域 成员变量在整个类中有效,局部变量只在声明他的方法中有效 2:优先级 成员变量和局部变量同名时,在局部变量的作用域内优先使用局部变量 3:初始值 成员变量系统自动初始化 局部变量需要自己初始化后才能使用

3.什么是方法重载?

答:在同一个类中,方法名相同,参数列表不同,与返回值无关

4.this代表什么?作用是什么?

答:本类对象 作用:用来调用类中的成员 区分成员变量和局部变量

5.定义三个重载方法max(),

第一个方法求两个int值中的最大值, 第二个方法求两个double值中的最大值, 第三个方法求三个double值中的最大值, 并分别调用三个方法。 分析: 三个方法: 方法一:max(int a,int b) 判断大小 方法二:max(double a,double b) 判断大小 方法三:max(double a,double b,double c) 判断大小 答:

public int max(int a,int b){ int max =0; if(a>b){ max =a; }else if(a <b){ max = b; } return max; } public double max(double a,double b){ double max =0.0; if(a>b){ max =a; }else if(a<b){ max =b; } return max; } public double max(double a,double b,double c){ double max = a; if(max <b){ max =b; } if(max <c){ max = c; } return max; }

6.定义一个网络用户类:

属性: 用户ID 用户密码 email地址 构造:为属性初始化 构造一:所有属性初始化 构造二:用户ID、用户密码必填,email地址不用填,缺省是用户ID加上字符串”@163.com“;

答:

class User{ private int id; private String password; private String email; public User(int id , String password,String email){ this.id = id; this.password = password; this.email = email; } public User(int id,String password, String email){ this.id = id; this.password = password; this.email = this.id+"@163.com"; } }

第三期

1.什么是方法重载?

答:在同一个类中,方法名相同,参数列表不同,与返回值无关

2.this代表什么?作用是什么?

答:本类对象 用来调用类中的成员,区别成员变量和局部变量

3.访问修饰符有几种?可见性范围?

答: private 本类中 默认 本类 本包 protected 本类 本包 子类 public 都可见

4.静态方法和实例方法区别?

答: 静态方法 只能访问静态成员 实例方法能访问静态和非静态成员 静态方法不能使用this ,super 实例方法可以使用 this,super

5.定义一个网络用户类:

属性: 用户ID 用户密码 email地址 构造:为属性初始化 构造一:所有属性初始化 构造二:用户ID、用户密码必填,email地址不用填,缺省是用户ID加上字符串”@163.com“;

答:

class WebUser{ private int id ; private String password; private String email; public WebUser(int id,String password,String email){ this.id = id; this.password = password; this.email = email; } public WebUser(int id ,String password){ this.id = id; this.password = password; this.email = this.id +"@163.com" } }

6.单例模式:饿汉式?

答:

class Window{ private static Window win = new Window(); private Window(){}; public static Window getWindow(){ return win; } }

第四期

1.什么是重写?

答:子类对父类允许的方法 的实现过程进行重写,返回值和参数不能改变

2.final 和 finalize()的作用?

答:finalize:清理资源,垃圾回收器,释放对象前调用 final修饰类时,不能被继承 final修饰父类的方法时,子类不能重写这个方法 final修饰变量时,基本数据类型的变量一旦被赋值,不能改变,引用类型的,属性不能改变,可以改变其属性值

3.抽象类 和 接口 的异同?

答:相同点:都不能创建对象 子类或实现类必须全部重写父类或父接口的全部抽象方法 不同点: 语法: 抽象类和接口中的成员不同 接口可以实现多继承 概念:抽象类只定义了类的抽象行为,没有具体的实现相应的行为 接口时一组规则的封装 设计:接口是功能模块的连接,降低耦合 抽象类是一个模板,可以说是一个半成品,子类可以继承父类的功能,也可以添加自己的功能

4.多态实现定义一个游泳Swimming接口:

使用多态实现:鱼Fish 可以游泳; 人Person 可以游泳; 答:

interface Swimming { void swim(); } class Fish implements Swimming{ @Override public void swim(){ System.out.println("鱼游泳")} } class Person implements Swimming{ @Override public void swim(){ System.out.println("人游泳") } } public class TestSwim{ public static void main(String [] args){ Swimming sw = new Fish(); sw.swim(); Swimming sw2 = new Person(); sw2.swim(); } }

5.输入一行字符串,编写代码实现取左串:

输入一行字符串,编写代码实现取左串。 public String left(String 字符串,int 取几个字符){} 答:

class StringLeft{ public String left(String str,int num){ return str.substring(0,num); } } public class TestString{ public static void main(String [] args){ Scanner superman = new Scanner(); System.out.println("--输入一行字符串"); String s = superman.next(); StringLeft sl = new StringLeft(); System.out.prinln(sl.left(s,4)); } }

第五期

1.String,StringBuffer,StringBuider区别?

答:String 为字符串常量,不可更改; StringBuffer,StringBuilder是字符串变量,可以更改 StringBuffer线程安全,速度慢 StringBuilder线程非安全,速度快

2.final,finalize,finally区别?

答:final修饰类:不能被继承, 修饰方法,子类不可重写, 修饰变量,基本数据类型的,一旦赋值,不可修改,引用类型的,属性不能更改,属性值可变 finalize:垃圾回收器,清理资源,在释放对象前调用 finally:一般用域try-catch代码块中,finally中的代码不管是否出现异常都会被执行

3.throw和throws区别?

答: throw:自己抛出某种特定类型的异常 throws:声明异常

4.写出所有的包装类名称?

答:Byte,Short,Integer,Long,Float,Double, Character,Boolean

5.代码实现:

定义一个学员类,有属性age,编写代码,使学员具备 按照年龄升序排序的能力。

答:

class Student implements Comparable<Student>{ private int age; public int getAge() { return age; } public Student(int age) { this.age = age; } @Override public String toString() { return "年龄是:" + age; } @Override public int compareTo(Student o) { return this.age - o.age; } } public class TestStudent { public static void main(String[] args) { Student gj = new Student(22); Student yk = new Student(21); Student hr = new Student(23); Student [] stu = {gj,yk,hr}; Arrays.sort(stu); Arrays.stream(stu).forEach(System.out::println); } }

第六期

1.List、Set作用?

答:都实现了Collection接口 list 允许数据重复存储,有序 set 数据是唯一的,无序

2.ArrayList 、Vector和 LinkedList区别?

答:ArrayList 、Vector底层数据结构是数组,LinkedList底层诗句结构式链表 Vector 线程非安全的,ArrayList 、LinkedList是线程安全 ArrayList 、Vector适合进行频繁的查询,LinkedList适合进行增删操作

3.HashSet、LinkedHashSet、TreeSet区别?

答: HashSet底层是hash表,唯一的,无序的, LinkedHashSet底层是链表,唯一的,有序的,可以根据元素添加的顺序进行维护 TreeSet底层是二叉树,唯一的,有序的,可以自己定制顺序

4.代码实现:TreeSet集合

向TreeSet集合中添加 3只小猫的信息,小猫信息包括 名称、类型、年龄,并且按照年龄自然升序排序。 添加的三只小猫信息: 大花 波斯猫 1 ;二花 梨花猫 2 ; 小花 短毛猫 2; 要求三只小猫的信息都要添加成功。

import java.util.TreeSet; class Cat implements Comparable<Cat>{ private String name; private String type; private int age; public Cat(String name, String type, int age) { this.name = name; this.type = type; this.age = age; } public String getName() { return name; } public String getType() { return type; } public int getAge() { return age; } @Override public String toString() { return "Cat [name=" + name + ", type=" + type + ", age=" + age + "]"; } @Override public int compareTo(Cat o) { int temp = this.age -o.age; //这一句是重点,这一句是用来保证相同的数字在排序时不会被吞掉 int temp1 = temp==0 ? this.name.compareTo(o.name) : temp; return temp1; } } public class TestCat { public static void main(String[] args) { Cat erhua = new Cat("二花","梨花猫",2); Cat dahua = new Cat("大花","波斯猫",1); Cat xhua = new Cat("小花","短毛猫",2); TreeSet<Cat> ts = new TreeSet<>(); ts.add(dahua); ts.add(erhua); ts.add(xhua); ts.forEach(System.out::println); } }

5.代码实现:读写文件

读取一个mp4文件,把此文件写入到new.mp3文件中,考虑到文件效率问题。 答:

public class TestFileInputStream { public static void main(String[] args) throws Exception { FileInputStream fi = new FileInputStream("f:/data/1.mp4"); BufferedInputStream bfi = new BufferedInputStream(fi); FileOutputStream fo = new FileOutputStream("f:/data/new.mp4"); BufferedOutputStream bfo = new BufferedOutputStream(fo); int temp ; while((temp = bfi.read())!=-1){ bfo.write(temp); } bfo.flush(); bfi.close(); bfo.close(); } }

最新回复(0)