Java总结第五天

1、引用传递
见N1
2、 this关键字
表示类中的属性和调用方法
调用本类中的构造方法
表示当前对象
 
3、static关键字
使用static声明属性
static声明的全局属性
使用static声明方法
直接通过类名调用
注意:
使用static方法的时候,只能访问static声明的属性和方法,而非static声明的属性和方法是不能访问的

4、继承的实现
基本概念
扩展父类的功能
java中使用extends关键字完成继承
class 子类 extends 父类 { }
5、继承的限制
在java中只允许单继承,可以多层继承
子类不能直接访问父类的私有成员
6、子类对象的实列化
子类对象实列化之前,必须先调用父类中的构造方法,之后调用子类的构造方法
7、重写与super
在继承中,也存在着重写的概念,其实就是子类定义了和父类同名的方法
定义
方法名称相同,返回值类型相同,参数也相同
重写限制
被子类重写的方法不能拥有比父类方法更加严格的访问权限
访问权限
private 《 default 《 public
8、super关键字
强行调用父类的方法执行
super不一定在重写中使用,也可以表示那些方法时从父类中继承而来的

9、重写与重载的对比(自己百度)-->重要

10、试着比较C++和Java的语法。

代码:
1、
package N05;

class Num{
int temp = 1;
}

class Str{
String temp = "jlran";
}

public class N1 {

/**引用传递
* @param args
*/
public static void main(String[] args) {
Num num = new Num();
num.temp = 10;
System.out.println(num.temp);
tell(num); //引用传递
System.out.println(num.temp);
//String类型不可变
String str1 = "hello";
str(str1);
System.out.println(str1);
//
Str st = new Str();
st.temp = "Hello";
System.out.println(st.temp);
st(st);
System.out.println(st.temp);
}
public static void tell(Num num)
{
System.out.println("tell:"+num.temp);
num.temp = 20;
}
public static void str(String str)
{
str = "jlran";
}
public static void st(Str str)
{
str.temp = "好好学习,天天向上";
}
}

2、
package N05;

class People01{
private String name;
private int age;
public People01() {
System.out.println("我是无参构造方法");
}
public People01(String name, int age)
{
this(); // 调用本类中的构造方法 必须放在前面
this.name = name; //表示类中的属性和调用方法
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void tell()
{
System.out.println("name:" + name + " age:" + age);
}
public void tell1()
{
System.out.println(this);//表示当前对象
}
}

public class N2 {

/**this关键字
* @param args
*/
public static void main(String[] args) {
People01 peo = new People01("张三",20);
peo.tell();
//表示当前对象
System.out.println(peo);
peo.tell1();
}

}


3、
package N05;

class People02{
static int age = 10;
String name;
public void tell()
{
System.out.println("name:" + name + "age:" + age);
}
}

public class N3 {

/**static关键字
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
People02 pe = new People02();
pe.name = "01";
// pe.age = 20;
People02.age = 20;
pe.tell();
People02 pe1 = new People02();
pe1.name = "02";
pe1.tell();
}

}

4、
package N05;


class Parent01{
String name;
int age;
private int in = 50;
public int getIn() {
return in;
}
public void setIn(int in) {
this.in = in;
}
}

class Child01 extends Parent01{
String type;
public void tell()
{
System.out.println("name:" + name + " age:" + age + " type:" + type); 
}
}

class Child02 extends Child01{
}

//class Child03 extends Child01,Child02{
// 只能单继承
//}

public class N4 {

/**
* @param args
*/
public static void main(String[] args) {
Child01 ch = new Child01();
ch.name = "展示";
ch.age = 20;
ch.type = "篮球";
ch.setIn(30); //私有属性通过get 和 set 方法访问
ch.tell();
}

}

5、
package N05;

class A{
public void tell01(){
System.out.println("我是父类");
}
}

class B extends A{
public void tell01(){
super.tell01(); //强行调用父类的方法执行
System.out.println("我是子类,重写了父类");
}
}

public class N5 {

/**重写与super
* @param args
*/
public static void main(String[] args) {
B b = new B();
b.tell01();
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值