5.18周测

1.分别写出分支结构,循环结构的语法格式。
循环结构:
while 循环
[html]  view plain  copy
  1. (init_statement);  
  2. while (test_expression) {  
  3.     body_statement;  
  4.     [iteration_statement];  
  5. }  
do…while 循环
[html]  view plain  copy
  1. (init_statement);  
  2. do {  
  3.     body_statement;  
  4.     [iteration_statement];  
  5. } while (test_expression)  
for 循环
[html]  view plain  copy
  1. for ([init_statement]; test_expression; [iteration_statement]) {  
  2.     body_statement;  
  3. }  
foreach循环
[html]  view plain  copy
  1. for (type element : array | collections) {  
  2.     element...  
  3. }  
分支结构:
if 语句
[html]  view plain  copy
  1. if (logic expression) {  
  2.     statement;  
  3. }  
  4. if (logic expression) {  
  5.     statement;  
  6. } else {  
  7.     statement;  
  8. }  
  9. if (logic expression) {  
  10.     statement;  
  11. } else if (logic expression) {  
  12.     statement;  
  13. } else {  
  14.     statement;  
  15. }  
switch 语句
[html]  view plain  copy
  1. switch (expression) {  
  2.     case condition1 : {  
  3.         statement;  
  4.         break;  
  5.     }  
  6.     case condition2 : {  
  7.         statement;  
  8.         break;  
  9.     }  
  10.     default : {  
  11.         statement;  
  12.         break;  
  13.     } }  
2.写出控制循环结构的相关语句,并描述其作用。
break:在循环体中,使用 break 关键字跳出整个循环。
[html]  view plain  copy
  1. int flag = 6;  
  2. int sum = 0;  
  3. for (int i = 1; i <= 10; i++) {  
  4.     sum = sum + i;  
  5.     if (i == flag) {  
  6.         break;  
  7.     }  
  8. }  
  9. System.out.println(sum); // 21  
continue:在循环体中,使用 continue 跳出本次循环,循环还会继续执行。
[html]  view plain  copy
  1. int sum = 0;  
  2. for (int i = 1; i <= 10; i++) {  
  3.     if (i % 2 == 0) {  
  4.         continue;  
  5.     }  
  6.     sum = sum + i;  
  7. }  
  8. System.out.println(sum); // 25  
3.数组初始化的方式有哪些?请举例说明。
静态初始化:int[] arrs = {1, 2, 3, 4};
动态初始化:type[] arrayName = new type[length];
4.请写出一个合理的数组,并使用foreach循环遍历到控制台。
[html]  view plain  copy
  1. int[] arrs3={1,3,7,9,11};  
  2. for(int a : arrs3) {  
  3.     System.out.println(a);  
  4. }  
5.类和对象是什么关系?类的命名规范是什么?
类是一群对象的特征母版,对象是类的具体实例。
类是一群对象的抽象。
类名的定义要符合 Java 的标识符命名规范,类名首字母大写,如果多个单词,使用驼峰命名法则(每个独立单词首字母大写)
6.类的成员有哪些?用于成员变量的修饰符有哪些?
类的三大部件:成员变量、方法、构造器。
修饰符: public protected private static final,其中 public protected private 只允许出现一个
7.成员变量包含哪些?命名规范分别是什么?
成员变量包括类变量和实例变量命名可以包括这4种字符:字母、下划线、$、数字;开头不能是数字;不能是关键字
用 static 修饰的为类变量,非 static 修饰的为实例变量。访问变量的语法:
    类.成员变量
    实例(对象).成员变量
8.请书写定义方法的语法?
    [修饰符] 方法的返回值数据类型 方法名(形参列表) {
    方法体语句;
    }
9.请书写构造器定义的语法?构造器的用途是什么?
[修饰符] 类名([参数列表]) {}
构造器的最大用处就是创建对象,在调用构造器创建对象的时候,系统会为每个对象的实例变量设置默认值,
通过构造器为成员变量定义初始化值,这也是构造器的最最重要的用途之一
10.请简述你对this的理解?写出使用this调用成员变量、方法、构造器的代码示例。
this 关键字,指向调用该方法的对象。根据 this 所在的位置,大致分为两种:
出现在构造器中:引用该构造器正在初始化的对象;
普通方法中:调用该方法的对象。
this调用成员变量,方法.
[html]  view plain  copy
  1.   private String name;  
  2.   private int age;  
  3.   prviate String code;  
  4. Student1(String name, int age, String code) {  
  5.     this.name = name;  
  6.     this.age = age;  
  7.     this.code = code;  
  8. }  
  9. String intro() {  
  10.     return "我叫" + this.name + ",我的学号是" + this.code + ",我今年" + this.age + "岁了。";  
  11. }  
  12. void listen() {  
  13.     System.out.println("自我介绍:" + this.intro() + "  " + this.name + "在上课。");  
  14.     return;  
  15. }  
this调用构造器
[html]  view plain  copy
  1. public Client1(int age, String name) {  
  2.         this(age);  
  3.         this.name = name;  
  4.     }  
  5. public Client1(int age) {         
  6.             this.age = age;  
  7.             }  
11.方法的参数其实传递的是什么类型的数据?如果传入的是基本数据类型,是否原数据的值会发生改变?如果传入的是引用数据类型呢?阐述原因。
参数的传递都是「值传递」,在调用方法的时候,参数会被创造出一个副本,原本的值是不会改变的。
 基本数据类型:方法调用时,实际参数把它的值传递给对应的形式参数,方法执行中形式参数值的改变不影响实际参数的值。
   引用数据类型:也称为传地址。方法调用时,实际参数的引用(地址,而不是参数的值)被传递给方法中相对应的形式参数,在方法执行中, 对形式参数的操作实际上就是对实际参数的操作,方法执行中形式参数值的改变将会影响实际参数的值。
如果参数类型是原始类型,那么传过来的就是这个参数的一个副本,如果在函数中改变了副本的值不会改变原始的值.
如果参数类型是引用类型,那么传过来的就是这个引用参数的副本,这个副本存放的是参数的地址。如果在函数中没有改变这个副本的地址,而是改变了地址中的值,那么在函数内的改变会影响到传入的参数。
12.方法签名是什么意思?
方法签名:包含方法名、形参列表。
13.什么是方法重载?
如果有两个方法的方法名相同,但参数不一致,那么可以说一个方法是另一个方法的重载。 具体说明如下:
方法名相同
方法的参数类型,参数个不一样
方法的返回类型可以不相同
方法的修饰符可以不相同
14.请分别说明类变量、实例变量、局部变量的生命周期?
 1,局部变量
 局部变量是指定义在方法或代码块中的变量,局部变量必须初始化,在方法或代码块内有效,之外则无效,方法执行开始入栈时创建,执行完毕出栈时销毁。
  2,实例变量
   其作用域受限定符限定,Private的只能在本类中使用,protected子类可用,public任何地方都可用。
   生命周期从对象创建开始到对象销毁结束。
 3,类变量
    类变量是指类的static变量,用类名可以直接访问。生命周期从加载类开始,到销毁类结束。
15.请书写标准的对下面两个成员变量的封装方法:
a) private String name;
  
[html]  view plain  copy
  1. 封装:public String getName() {  
  2.     return name;  
  3. }  
  4. public void setName(String name) {  
  5.     this.name = name;  
  6. }  

b)private String  getStuCode;

[html]  view plain  copy
  1. 封装:public String getStuCode() {  
  2.   
  3.     return stuCode;  
  4. }  
  5. public void setStuCode(String stuCode) {  
  6.     this.stuCode = stuCode;  
  7. }  

16.在企业开发中包名定义的规范是什么?

  命名分为三层:
第一层是企业的域名的反写
例如:com.ntqingniao
第二层是企业项目的名称
例如:com.ntqingniao.j96、com.ntqingniao.crm
第三层是企业项目模块的名称
例如:com.ntqingniao.j96.oop、com.ntqingniao.crm.base
17.我们一般在什么情况下使用import语句?如果不使用import语句,有什么其他的解决方案?
使用 import 关键字将本类要使用的其他包中的类进行引入。import 不是必须的,我们可以使用类全名的方式进行类的使用(一般不建议,太麻烦)。
18.访问控制修饰符有哪些?分别的控制范围是什么?
private default protected public
同一个类
同一个包  
子类    
全局范围      

19.对象在创建时的成员变量的默认值分别是什么?
如果是类类型的,没有定义默认值,那么成员变量的值为 null,如果是基本数据,没有定义默认值,那么成员变量的值是有意义的,比如 int 就是 0,boolean 就是 false。
20.请至少写出3个常用的JDK包。
java.lang:包含了 Java 语言的核心类库,如 String、System、Math等等,使用该包下的类是不需要显式引入;
java.util:包含了大量的工具类,如 Date、集合框架;
java.net:Java 网络编程的类和接口包;
java.io:Java 输入输出,如流等方面编程的包;
java.text:格式化方面的类;
java.sql:数据库编程方面的类和接口包;
java.awt:图形用户界面方面的类和接口包。
附加题:编程实现
55岁天秤座的张老师在给南通青鸟教育Java100班的学员上Java课
(提示:需要定义的类包括:老师类、学员类、课程类等等,要运用封装的概念,要有类变量的定义。最后需要通过方法在控制台打印出上述语句)
 Teacher类
[html]  view plain  copy
  1. package com.dodoke.test02;  
  2. /**  
  3.  * 创建教师类  
  4.  * @author 26966  
  5.  *  
  6.  */  
  7. public class Teacher {  
  8.     //声明教师的姓名,年龄,星座  
  9.     private String name;  
  10.     private int age;  
  11.     private String constellation;  
  12.     //创建构造器  
  13.       
  14.     public Teacher(String name, int age, String constellation) {  
  15.         super();  
  16.         this.name = name;  
  17.         this.age = age;  
  18.         this.constellation = constellation;  
  19.     }  
  20.     public Teacher() {  
  21.         super();  
  22.           
  23.     }  
  24.     //对属性值封装  
  25.     public String getName() {  
  26.         return name;  
  27.     }  
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.     public int getAge() {  
  32.         return age;  
  33.     }  
  34.     public void setAge(int age) {  
  35.         this.age = age;  
  36.     }  
  37.     public String getConstellation() {  
  38.         return constellation;  
  39.     }  
  40.     public void setConstellation(String constellation) {  
  41.         this.constellation = constellation;  
  42.     }  
  43. }  
Student类
[html]  view plain  copy
  1. package com.dodoke.test02;  
  2. /**  
  3.  * 学生类,属性包括姓名,班级  
  4.  * @author 26966  
  5.  *  
  6.  */  
  7. public class Student {  
  8.   private String name;  
  9.   private String calss;  
  10. public Student() {  
  11.     super();  
  12. }  
  13. public Student(String name, String calss) {  
  14.     super();  
  15.     this.name = name;  
  16.     this.calss = calss;  
  17. }  
  18. public String getName() {  
  19.     return name;  
  20. }  
  21. public void setName(String name) {  
  22.     this.name = name;  
  23. }  
  24. public String getCalss() {  
  25.     return calss;  
  26. }  
  27. public void setCalss(String calss) {  
  28.     this.calss = calss;  
  29. }    
  30. }  
Course类
[html]  view plain  copy
  1. package com.dodoke.test02;  
  2. /**  
  3.  * 建立课程类,属性为课程名  
  4.  * @author 26966  
  5.  *  
  6.  */  
  7. public class Course {  
  8.   private String name;  
  9.   //建立构造器  
  10. public Course() {  
  11.     super();  
  12. }  
  13. public Course(String name) {  
  14.     super();  
  15.     this.name = name;  
  16. }  
  17.   //封装  
  18. public String getName() {  
  19.     return name;  
  20. }  
  21. public void setName(String name) {  
  22.     this.name = name;  
  23. }  
  24. }  
School类
[html]  view plain  copy
  1. package com.dodoke.test02;  
  2. /**  
  3.  * 建立shool类,设定类变量name  
  4.  * @author 26966  
  5.  *  
  6.  */  
  7. public class School {  
  8.    static String name="南通青鸟教育";  
  9. //构造器  
  10. public School() {  
  11.     super();  
  12. }     
  13. }  

Main类

[html]  view plain  copy
  1. package com.dodoke.test02;  
  2. /**  
  3.  * 建立主类,来输出结果  
  4.  * @author 26966  
  5.  *  
  6.  */  
  7. public class Main {  
  8.     public static void main(String[] args) {  
  9.     //new 出老师,学员,课程对象  
  10.     Teacher zh=new Teacher("张老师",55,"天秤座");  
  11.     Student stu=new Student("学员","Java100班");  
  12.     Course cou=new Course("Java课");  
  13.     //输出结果  
  14.    System.out.println(zh.getAge()+"岁"+zh.getConstellation()+"的"  
  15. +zh.getName()+"在给"+School.name+stu.getCalss()+"的"+stu.getName()+"上"+cou.getName());  
  16.             }     
  17.     }  

猜你喜欢

转载自blog.csdn.net/readygoing/article/details/80384746