2012年10月6日
摘要: class TParent{ }class TChild extends TParent{ }派生类的构造函数,派生类调用基类的构造函数Super()public class TTest { public static void main(String[] args) { TChild child = new TChild("Child"); System.out.println(child.getName()); }}class TParent{ private String Name; public TParent(S... 阅读全文
posted @ 2012-10-06 15:42 kiny 阅读(200) 评论(0) 推荐(0) 编辑
摘要: public:任意位置的类访问private:类内部的方法访问protected:同一包中的访问,任意子类可以访问。如果什么也不写,默认是default。作用域当前类同包子孙类其他default√√××public√√√√private√×××protected√√√× 阅读全文
posted @ 2012-10-06 15:28 kiny 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 递归就是符合条件的情况下自己调用自己。一个例子说明,阶乘。n! = n*(n-1)public class TTest { public static void main(String[] args) { int num = 4; System.out.println(factorial(num)); } static int factorial(int n) { if (n==1) { return n; }else { return n*factorial(n-1)... 阅读全文
posted @ 2012-10-06 00:47 kiny 阅读(161) 评论(0) 推荐(0) 编辑
摘要: 有的类可以有构造函数,有的可以没有构造函数,如果没有构造函数,编译器会自动给类添加一个默认的构造函数,此构造函数什么也做。默认构造函数是无参的,因为调用它的时候什么也不需要去做,构造函数提供了在创建对象的时候初始化数据。当然还有一种初始化代码块的方法,初始化代码块在构造函数之前执行。看代码。public class TTest { public static void main(String[] args) { TPerson person; //初始化代码块和构造函数都没执行 person = new TPerson(); //打印结果 ... 阅读全文
posted @ 2012-10-06 00:01 kiny 阅读(315) 评论(0) 推荐(0) 编辑