Когда я вызываю конструктор в основной программе, отображаются значения по умолчанию. Я хочу, чтобы значения были инициализированы в Конструкторе по умолчанию(). Почему перегрузка конструктора не происходит, когда я явно определяю конструктор по умолчанию. Как мне вызвать их в основной программе?
Код:
`
package Constructors;
public class Constructor {
//Bank HDFC has few fields to be declared
public String customerName;
public int age;
private long adharNumber;
protected long accountNumber;
protected double bankBalance;
short birthDate;
String birthMonth;
int BirthYear;
public short branchCode;
public String IFSCCode;
public String branchLocation;
Constructor()
{
branchCode=20;
IFSCCode="HDFC000003";
branchLocation="Bengaluru";
customerName="Harish";
adharNumber=12456789;
bankBalance=1200.56;
birthDate=20;
birthMonth="OCT";
BirthYear=1994;
}
public void callConstructor()
{
branchCode=20;
}
public Constructor(long accountNumber,int age)
{
this.accountNumber=accountNumber;
this.age=age;
System.out.println("Details of "+accountNumber+" are as follow:");
}
public void getProtected()
{
System.out.println("bankBalance: "+bankBalance);
}
public void getPrivate(long adharNumber)
{
this.adharNumber=adharNumber;
System.out.println("adharNumber :"+adharNumber);
}
public void getPublic()
{
System.out.println("customerName :"+customerName);
System.out.println("branchCode :"+branchCode);
System.out.println("IFSCCode :"+IFSCCode);
System.out.println("branchLocation :"+branchLocation);
}`your text`
public void getDefault()
{
System.out.println("birthDate :"+birthDate);
System.out.println("birthMonth :"+birthMonth);
System.out.println("BirthYear :"+birthMonth);
}
public long getAdharNumber()
{
return adharNumber;
}
}
public class ConstructorTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Constructor c=new Constructor(22334455,29);
new Constructor();
c.getPublic();
c.getProtected();
c.getDefault();
c.getPrivate(0);
}
}`
Какую ошибку я совершаю? Пожалуйста, поправьте меня. Я впервые изучаю JAVA.
Ожидаемый результат:
customerName :Harish
branchCode :20
IFSCCode :HDFC000003
branchLocation :Bengaluru
bankBalance: 1200.56
birthDate :20
birthMonth :OCT
BirthYear :1994
adharNumber :12456789
Actual O/p:
Details of 22334455 are as follow:
customerName :null
branchCode :0
IFSCCode :null
branchLocation :null
bankBalance: 0.0
birthDate :0
birthMonth :null
BirthYear :null
adharNumber :0
Подробнее здесь: https://stackoverflow.com/questions/787 ... re-getting
Когда я вызываю конструктор в основной программе, отображаются значения по умолчанию. Я хочу, чтобы значения были инициа ⇐ JAVA
Программисты JAVA общаются здесь
1721976074
Anonymous
Когда я вызываю конструктор в основной программе, отображаются значения по умолчанию. Я хочу, чтобы значения были инициализированы в Конструкторе по умолчанию(). Почему перегрузка конструктора не происходит, когда я явно определяю конструктор по умолчанию. Как мне вызвать их в основной программе?
Код:
`
package Constructors;
public class Constructor {
//Bank HDFC has few fields to be declared
public String customerName;
public int age;
private long adharNumber;
protected long accountNumber;
protected double bankBalance;
short birthDate;
String birthMonth;
int BirthYear;
public short branchCode;
public String IFSCCode;
public String branchLocation;
Constructor()
{
branchCode=20;
IFSCCode="HDFC000003";
branchLocation="Bengaluru";
customerName="Harish";
adharNumber=12456789;
bankBalance=1200.56;
birthDate=20;
birthMonth="OCT";
BirthYear=1994;
}
public void callConstructor()
{
branchCode=20;
}
public Constructor(long accountNumber,int age)
{
this.accountNumber=accountNumber;
this.age=age;
System.out.println("Details of "+accountNumber+" are as follow:");
}
public void getProtected()
{
System.out.println("bankBalance: "+bankBalance);
}
public void getPrivate(long adharNumber)
{
this.adharNumber=adharNumber;
System.out.println("adharNumber :"+adharNumber);
}
public void getPublic()
{
System.out.println("customerName :"+customerName);
System.out.println("branchCode :"+branchCode);
System.out.println("IFSCCode :"+IFSCCode);
System.out.println("branchLocation :"+branchLocation);
}`your text`
public void getDefault()
{
System.out.println("birthDate :"+birthDate);
System.out.println("birthMonth :"+birthMonth);
System.out.println("BirthYear :"+birthMonth);
}
public long getAdharNumber()
{
return adharNumber;
}
}
public class ConstructorTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Constructor c=new Constructor(22334455,29);
new Constructor();
c.getPublic();
c.getProtected();
c.getDefault();
c.getPrivate(0);
}
}`
Какую ошибку я совершаю? Пожалуйста, поправьте меня. Я впервые изучаю JAVA.
Ожидаемый результат:
customerName :Harish
branchCode :20
IFSCCode :HDFC000003
branchLocation :Bengaluru
bankBalance: 1200.56
birthDate :20
birthMonth :OCT
BirthYear :1994
adharNumber :12456789
Actual O/p:
Details of 22334455 are as follow:
customerName :null
branchCode :0
IFSCCode :null
branchLocation :null
bankBalance: 0.0
birthDate :0
birthMonth :null
BirthYear :null
adharNumber :0
Подробнее здесь: [url]https://stackoverflow.com/questions/78796581/when-i-call-the-constructor-in-the-main-program-the-default-values-are-getting[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия