JAVA中初始化和清除、函数的重载、缺省构造函数

原创
2016/04/09 22:03
阅读数 334

※初始化和清除
As the computer revolution progress,
"unsafe" programming has become one
of the the major culprits that makes
programming expense.
×初始化和清楚是程序设计安全性的两个最
重要的问题。
×C++为我们引入了构造函数的概念,java也
沿用了这一概念,但新增了自己的垃圾收集器。
×java无析构函数

※用构造函数自动初始化
×如果某个类有一个构造函数,那么在创建对象
的时候,java会自动调用那个构造函数。
构造函数的名字和类的名字相同。
Case:SimpleConstructor.java
//SimpleConstructor

class Rock{
      Rock(){
       System.out.println("Creat Rock!");
       }
}
public class SimpleConstructor{
    public static void main(String[] args){
          for(int i=0;i<10;i++){
               new Rock();
           }
     }
}

※Methods overloading//函数的重载
one of the important features in any
programming language is the use of names.
×我们用名字引用或描述所有对象与方法。

Case: Overloading.java

//Overloading

class Tree{
      int height;
       Tree(){
         System.out.println("Planting a seedling");
         height=0;
       }
       Tree(int i){
          System.out.println("Creating new Tree
that is"+i+"feet tall");
          height=i;
        }

×如果 Tree t=new Tree(i);有参数的调用有参数的
×Tree t=new Tree(); 没参数的调用没参数

 

※缺省构造函数
×每个重载的方法都必须采用独一无二的
自变量类型列表。
×返回类型重载。
×如果一个类没有定义构造函数,则编译
程序会帮我们自动创建一个缺省构造函数。
×然而一旦定义了一个构造函数,就不会帮
我们自动生成一个。
(此与C++相同)

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
1
分享
返回顶部
顶部