JAVA构造方法及其重载
2016-11-23 17:49
260 查看
在没有给构造方法的时候,系统会自动给出一个默认的构造方法
但是注意在给出任意一个构造方法的时候,系统将不会提供构造方法
构造方法重载的时候,会根据方法参数来确定使用哪一个构造方法
下面看例子
class Student {
private String name ;
private int age;
public Student() {
System.out.println("这是默认的构造方法");
}
public Student(String name) {
this.name = name;
}
public Student(int age) {
this.age = age;
}
public Student(String name, int age) {
this.age = age;
this.name = name;
}
public void show() {
System.out.println("name: "+name+"----age: "+age);
}
}
class ConstructDemo {
public static void main(String[] args) {
//Student s = new Student();
//System.out.println(s);
Student s0 = new Student();
Student s1 = new Student("刘轲");
s1.show();
Student s2 = new Student(30);
s2.show();
Student s3 = new Student("刘轲",30);
s3.show();
}
}
但是注意在给出任意一个构造方法的时候,系统将不会提供构造方法
构造方法重载的时候,会根据方法参数来确定使用哪一个构造方法
下面看例子
class Student {
private String name ;
private int age;
public Student() {
System.out.println("这是默认的构造方法");
}
public Student(String name) {
this.name = name;
}
public Student(int age) {
this.age = age;
}
public Student(String name, int age) {
this.age = age;
this.name = name;
}
public void show() {
System.out.println("name: "+name+"----age: "+age);
}
}
class ConstructDemo {
public static void main(String[] args) {
//Student s = new Student();
//System.out.println(s);
Student s0 = new Student();
Student s1 = new Student("刘轲");
s1.show();
Student s2 = new Student(30);
s2.show();
Student s3 = new Student("刘轲",30);
s3.show();
}
}
相关文章推荐
- 【C#基础】方法及其调用、构造方法、out与ref参数及其返回值、方法重载、静态方法等简单介绍
- java 构造方法重载
- 构造方法,重载,static,math类(java基础知识七)
- java中构造方法及其作用
- java学习日记_58:String类的概述及其构造方法
- 【C#基础】方法及其调用、构造方法、out与ref参数及其返回值、方法重载、静态方法等简单介绍
- Java学习----构造方法的重载
- Java中构造方法、类方法、final方法的重载与覆盖问题
- Java面向对象02-方法的签名,重载,构造方法和引用类型数组
- Java---java中构造方法及其作用
- java-05方法重载、覆盖、参数传递、构造方法
- Java中构造方法与重载
- Java构造方法重载的注意事项
- 【幻化万千戏红尘】qianfengDay06-java基础学习:重载、方法、类、对象、构造方法
- [Java] this 调用重载构造方法
- Lesson_for_java_day09--继承、实例化、构造方法、覆盖、重载、this关键字、super关键字、全局变量和局部变量
- java学习之旅35--面向对象_08_方法重载_构造方法重载
- JAVA中StringBuffer的构造及其方法
- 在Java中子类能否重载父类的构造方法?
- Java基础之构造方法及其应用