博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Constructors
阅读量:4885 次
发布时间:2019-06-11

本文共 1814 字,大约阅读时间需要 6 分钟。

  1. 构造函数是一种特殊的方法,和class有相同的名字
  2. 构造函数是用来初始化类的实例变量的
  3. 构造函数有两种类型:默认的构造函数和参数化的构造函数
ØConstructor is a special method that has the same name as that of a class
ØThe constructor is used to initialize the instance variables of the class 
ØTypes of  constructors:
§Default constructor
§Parameterized constructor
 

Parameterized constructor will be discussed along with method overloading

Default Constructor

ØIf the programmer does not code the constructor explicitly, the system provides a default constructor
§This initializes the instance variables to their default values
ØProgrammer can redefine the default constructor
§Such a constructor does not take any arguments
§The instance variables can be initialized by the user explicitly inside this constructor
 

Retail Application – Case Study

class Customer{

  private int customerId;

  public int getCustomerId(){

  return customerId;

  }

}

class Retail{

public static void main(String args[]){

  Customer custObj=new Customer();

  System.out.println("Customer Id:"+

  custObj.getCustomerId());

}

}

Output:

  Customer Id: 0

Note that the system provides a default constructor

 

class Customer{

  private int customerId;

  public Customer(){

  customerId=1000;

  }

  public int getCustomerId(){

  return customerId;

  }

}

class Retail{

public static void main(String args[]){

  Customer custObj=new Customer();

  System.out.println("Customer Id:“+  custObj.getCustomerId());

  }

}

Retail Application – Case Study 

class Customer{

  private int customerId;

  public Customer(){

  customerId=1000;

  }

  public int getCustomerId(){

  return customerId;

  }

}

class Retail{

public static void main(String args[]){

  Customer custObj=new Customer();

  System.out.println("Customer Id:“+  custObj.getCustomerId());

  }

}

Output:

  Customer Id: 1000

Note that the default constructor is redefined

 

 

转载于:https://www.cnblogs.com/yqskj/articles/2053271.html

你可能感兴趣的文章
VS2013 调试时出现“表达式计算器中发生内部错误”的问题解决办法
查看>>
[C++]让CPU使用率曲线呈现为正弦曲线(一)
查看>>
20155313 2016-2017-2 《Java程序设计》第六周学习总结
查看>>
还是畅通工程
查看>>
Codeforces 585D. Lizard Era: Beginning(meet in the middle)
查看>>
Python_UUID模块
查看>>
ModelSim仿真教程
查看>>
Android 启动之 Bootloader(uboot)
查看>>
[CentOS7] iconv编程转换
查看>>
EJB中常用的设计模式
查看>>
C# 把第一次出现的重复数字移动到最后
查看>>
Hadoop入门第五篇:Hive简介以及部署
查看>>
最大熵学习笔记(四)模型求解
查看>>
看源码
查看>>
《软件需求工程》读后感06
查看>>
javascript数组操作
查看>>
【mysql】mysql内置函数
查看>>
分析函数
查看>>
Spark学习笔记(一)
查看>>
虫食算
查看>>