java学习笔记(一)

直接调用方法

 1 //:initialization/Practice3Initialzation.java
 2 //练习:创建一个带默认构造器的类在构造器中打印一条消息,为这个类创建一个对象
 3 package thinkingInJava;
 4 
 5 class ConstructorDefault{
 6     //定义接受参数的变量,因为要在不同的方法中使用,故定义为全局变量
 7     String str;
 8     ConstructorDefault(){
 9         Print.print("这是一个默认构造器的打印");
10         Print.print("");
11     }
12     ConstructorDefault(String s){
13         str = s;
14         Print.print("一个带字符串参数的构造器");
15         Print.print("接受的参数: " + str);
16     }
17     /*void printArgs(){
18         Print.print("一个带字符串参数的构造器");
19         Print.print("接受的参数: " + str);
20         Print.print("");
21     }*/
22 }
23 public class Practice3Initialzation {
24     public static void main(String[] args) {
25         //直接调用默认构造器
26         new ConstructorDefault();
27         /*ConstructorDefault name = new ConstructorDefault("How are you!");
28         name.printArgs();*/
29         //直接调用构造方法
30         new ConstructorDefault("Fine,thank you");
31     }
32     
33 }

 

posted on 2016-12-30 09:27  知止而后有定  阅读(178)  评论(0)    收藏  举报

导航