首页 > 代码库 > 总结一下这几节Java课的...重点!!!

总结一下这几节Java课的...重点!!!

1.定义一个Person类,包含两个私有的属性(name、age)、一个含参的方法setValue(int age,String name)、一个不含参方法setValue()和一个普通方法tell()。

定义一个测试类Test,实例化一个Person类的对象,名为p;对两个属性赋值并输出结果。

 1 class Person
 2 {
 3     private int age;
 4     private String name;
 5     public void tell()
 6     {
 7         System.out.print("姓名:"+name+",年龄:"+age);
 8     }
 9     public void setValue(int age,String name)
10     {
11         this.age=age;
12         this.name=name;
13     }
14     public void setValue()
15     {
16         this.name="张三";
17         this.age=21;
18     }
19 }
20 public class Test
21 {
22 
23     public static void main(String[] args) 
24     {
25         Person p = new Person();
26         p.setValue(21,"Mary");
27         p.tell();
28     }
29 }

2.定义一个Student类,继承自Person类,含有一个protected访问权限的字符型属性sex,定义Student类的构造方法,并覆写父类的tell()方法,实现对三个属性的赋值和输出。

 1 class Person
 2 {
 3     private int age;
 4     private String name;
 5     public void tell()
 6     {
 7         System.out.print("姓名:"+name+",年龄:"+age);
 8     }
 9     public void setValue(int age,String name)
10     {
11         this.age=age;
12         this.name=name;
13     }
14     public void setValue()
15     {
16         this.name="张三";
17         this.age=21;
18     }
19 }
20 class Student extends Person
21 {
22     protected char sex;
23     Student(int age,String name,char sex)
24     {
25         super.setValue(age,name);
26         this.sex=sex;
27     }
28     public void tell()
29     {
30         super.tell();
31         System.out.println(",性别:"+sex);
32     }
33 }
34 public class Test
35 {
36 
37     public static void main(String[] args) 
38     {
39         Student s = new Student(20,"Mary",‘女‘);
40         s.tell();
41     }
42 }

3.数据库访问的实现

 1 import java.sql.Statement;
 2 import java.sql.Connection;
 3 import java.sql.ResultSet;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 
 7 public class MySQL
 8 {
 9     public static void main(String[] args){
10         Connection conn = null;
11         Statement stmt = null;
12         ResultSet rs =null;
13         try
14         {
15             Class.forName("com.mysql.jdbc.Driver");
16             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentInfo","root","123456");
17             stmt = conn.createStatement();
18             rs = stmt.executeQuery("select * from student");
19             while(rs.next())
20             {
21                 String no = rs.getString(1);
22                 String name = rs.getString("sName");
23                 String sex = rs.getString(3);
24                 int age = rs.getInt(4);
25                 String dept = rs.getString("dept");
26                 System.out.println(no+"__"+name+"__"+sex+"  "+age+"  "+dept+"__");
27             }
28         }
29         catch(Exception e)
30         {
31             e.printStackTrace();
32         }
33         finally
34         {
35             try
36             {
37                 if(rs !=null) rs.close();
38                 if(stmt!=null) stmt.close();
39                 if(conn !=null) conn.close();
40              }
41             catch(SQLException e)
42             {
43                 e.printStackTrace();
44             }
45         }
46     }
47 }

这是重点!这是重点!这是重点!重要的事情说三遍!!!

总结一下这几节Java课的...重点!!!