yinmingzheng13 发表于 2008-4-14 20:09

构造方法也需要返回值类型吗? ******谢! 写了那么的方法是否有点重复,能否就重要的几种详细说明

class Person
{
        String name;
        int age;
       
       
        static int x;
       
        Person( String n,int a)
        {
                name = n;
                age = a;
        }
       
        Person( String n)
        {
                name = n;
                age =-1;
        }
       
        Person( int age,String name)
        {
                this.age = age;
                this.name = name;
        }
       
        Person()
        {
                this(0,"");
        }
       
        void sayHello()
        {
                System.out.println("Hello!My name is"+ name);
        }
       
        void sayHello( Person another)
        {
                System.out.println("Hello,"+another.name+"!My name is "+name);
        }
       
        boolean isOlderThan( int anAge)
        {
                bloolean flg;
                if( age> anAge ) flg=true; else flg=false;
                return flg;
        }
       
       
        public static void main (String[] args)
        {
                System.out.println("Hello World!");
               
        }
       
        class Student extends Person
        {
                String school;
                int score;
                //void sayHello(){
                        //System.out.println("Hello! My name is "+ name+ ".My school is"+ school);//}
        }
       
        void sayHello( Student another)
        {
                System.out.println("Hi");
                if(school==another.school) System.out.println("Schoolmate");
        }
       
        boolean isGoodStudent()
        {
                return score>=90;
        }
    void testThisSuper()
    {
            a = age;
            a = this.age;
            a = super.age;
    }
   
    void sayHello()
    {
            super.sayHello();
            System.out.println("My school is" +school);
    }
   
   
    Student(String name,int age,String school)
    {
            super(name, age);
            this.school = school;
    }
   
    Student(){}
   
    public void main( String [] args)
    {
            Person p = new Person("Liming",50);
            Student s =  new Student("Wangqiang",20,"PKU");
            Person p2 = new Student("Zhangyi",18,"THU");
            Student s2 = (Student) p2;
    }
}

Character 发表于 2008-4-16 10:31

构造方法不允许有返回值。如果有返回值。那就成为了普通的方法。在构造的时候会使用JDK提供的隐式构造方法。你写的前四种构造方法通过不同的参数表而形成了重载。实际上我觉得重载的意义并不是非常大。可能是我接触的较少。觉得重写的意义要比重载的意义大的多。而你下边方法名重复的方法。就是一种典型的重写。但是一般重写是不会出现在同一个类里的。一般都是具象类实现抽象类。或者根据需求子类重新定义父类同名方法的一种形式。

sunkaidong 发表于 2008-4-16 10:41

构造函数是要重载的.要不一旦参数个数或类型不对.是没办法用的..java在你有构造函数的时候不会像c++那样提供不带参数构造函数...

aipb2007 发表于 2008-4-16 11:03

java在你有构造函数的时候不会像c++那样提供不带参数构造函数...

这句话,你可以验证一下。

sunkaidong 发表于 2008-4-16 11:10

api兄弟是说我吗?

class Explicit{
     static  Test3 b=new Test3();                                 
    Explicit(String s){
        System.out.println("Explicit");
    }
    Test3 a=new Test3();
}
class Test3{
    Test3(){
        System.out.println("Test31");
    }
    public String toString()
    {
            return getClass().getName();
            }
}
class Test{
    public static void main(String[] args){
        Explicit e=new Explicit();
        System.out.println(Explicit.b);
    }
}

[[it] 本帖最后由 sunkaidong 于 2008-4-16 11:14 编辑 [/it]]

aipb2007 发表于 2008-4-16 12:23

回复 5# 的帖子

我是说验证后半句,c++中

sunkaidong 发表于 2008-4-16 12:33

恩.看来是我记错了.

yinmingzheng13 发表于 2008-4-16 13:27

编译时描述说:   
         ....................................
         ....................................
         ....................................
         void sayHello()
    {
            super.sayHello();
            System.out.println("My school is" +school);
    }
   
   
    Student(String name,int age,String school)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    {
            super(name, age);
            this.school = school;
    }
   
    Student(){}
    ~~~~~~~~~~~
    public void main( String [] args)
    {
            Person p = new Person("Liming",50);
            Student s =  new Student("Wangqiang",20,"PKU");
            Person p2 = new Student("Zhangyi",18,"THU");
            Student s2 = (Student) p2;
    }
}
                打波浪线的两句:  方法生命无效;需要返回值类型

sunkaidong 发表于 2008-4-16 13:35

class Person
{
    String name;
    int age;
   
   
    static int x;
   
    Person( String n,int a)
    {
        name = n;
        age = a;
    }
   
    Person( String n)
    {
        name = n;
        age =-1;
    }
   
    Person( int age,String name)
    {
        this.age = age;
        this.name = name;
    }
   
    Person()
    {
        this(0,"");
    }
   
    void sayHello()
    {
        System.out.println("Hello!My name is"+ name);
    }
   
    void sayHello( Person another)
    {
        System.out.println("Hello,"+another.name+"!My name is "+name);
    }
   
    boolean isOlderThan( int anAge)
    {
        boolean flg;
        if( age> anAge ) flg=true; else flg=false;
        return flg;
    }
   
   
   }
   
    class Student extends Person
    {
        String school;
        int score;
        //void sayHello(){
            //System.out.println("Hello! My name is "+ name+ ".My school is"+ school);//}
   // }
   
    void sayHello( Student another)
    {
        System.out.println("Hi");
        if(school==another.school) System.out.println("Schoolmate");
    }
   
    boolean isGoodStudent()
    {
        return score>=90;
    }
    void testThisSuper()
    {
        //a = age;
        //a = this.age;
       // a = super.age;
    }
   
    void sayHello()
    {
        super.sayHello();
        System.out.println("My school is" +school);
    }
   
   
    Student(String name,int age,String school)
    {
        super(name, age);
        this.school = school;
    }
   
    Student(){}
   
    public static void main( String [] args)
    {
        Person p = new Person("Liming",50);
        Student s =  new Student("Wangqiang",20,"PKU");
        Person p2 = new Student("Zhangyi",18,"THU");
        Student s2 = (Student) p2;
    }
}

yinmingzheng13 发表于 2008-4-20 19:45

[tk27]    上面的大哥 你在哪地方做了修改?

bkillbelong 发表于 2008-4-20 22:13

回复 6# 的帖子

java和C++的构造方法一旦重载,编译器就不再提供默认的构造方法了,仁兄又加深了我的记忆,谢了

[[it] 本帖最后由 bkillbelong 于 2008-4-20 22:14 编辑 [/it]]

页: [1]

编程论坛