java构造方法与super

为什么使用java构造方法,个人认识

构造方法属于方法的一类。特点:在实例对象的时候调用构造方法,有的时候,要把类中出成员封装。private后,只有被该类的成员使用

实例化时赋值

class Student
{
private int age;
public Student(int n)  
{
this.age=n;
System.out.println(age);    //构造方法中输出age值也可以在用个方法
}
}
class Test002
{
public static void main(String[] args)
{

new Student(5);  //Student stu = new Student(5);也是可以,今天刚刚明白 Student stu 是一个Student类型的变量stu     new Student()建立古Student类型对象

                                                                                                               等号  Student对象内存中变量地址赋值给stu


}
}

super

继承

class Animal
{
public Animal(int x,int y)
{
int temp=x+y;
System.out.println(temp);
}
}
class Dog extends Animal
{
public Dog(int n,int y)    //在Dog类的构造方法中继续!!!!构造方法中继承
{
super(n,y);  //惊奇,本来我准备输入(5,8);突然想到我想通过main()来输入,就输入Dog类构造方法的参数。用Dog类的参数,Animal类方法。惊喜一个居然成功了。发现,只要你符合基本结构,灵活运用。快毕业了,不知道自己能不能找个实习单位,加油
}
}
class Test002
{
public static void main(String[] args)
{
new Dog(5,8);
}
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值