文章目录
语言基础
变量
概念
存储数据的基本单元,可以通过用户选择改变。
全局变量、局部变量。
全局变量
java中的全局变量是放在静态方法区中的,所有的实例对象会共享同一份全局变量。
package com.company;
public class Work6 {
int a;
public static int b;
}
局部变量
局部变量的位置处于方法内,只在方法内通用,其他方法不可调用,在使用前必须初始化
- 有效范围:开始行数,所在的代码块位置
package com.company;
public class Work6 {
public static void main(String[] args) {
int a;//局部变量
a = 10;
}
}
实例变量
在内部类中,可以不进行初始化即可被调用输出,结果为null
package com.company;
public class Work6 {
public static void main(String[] args) {
int a;//局部变量
a = 10;
Wjs w = new Wjs();
System.out.println(w.str);//结果为null
}
}
class Wjs{//实例类
String str;//实例变量
int b;
}
声明
- 先声明,再赋值:【常用】
- 数据类型 变量名;
- 变量名 = 值;
- 声明并赋值:【常用】
- 数据类型 变量名 = 值;
- 多个同类型变量的声明与赋值:【了解】
- 数据类型 变量1 , 变量2 , 变量3 = 值3 , 变量4 , 变量5 = 值5;
数据类型
常用
基本数据类型
int,long,float,double,boolean,char
引用数据类型
String
Unicode编码表
网友链接:(36条消息) Unicdoe【真正的完整码表】对照表(二)汉字Unicode表_郭晓东的专栏-CSDN博客_unicode对照表
ASCII字符表
运算符
算术运算符
数学算法、++(在变量前:先加再进行下一步,否则先进行下一步再自加)、–(同左)
赋值运算符
等号右边赋值给等号左边。
| [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZvUmQJDc-1627868613278)(E:\飞秋\feiq\Recv Files\1\2.0 语言基础(已做笔记)\Pictures\赋值运算符.PNG)] |
关系运算符
关系运算符:两个操作数进行比较。
| [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xsNtDEFE-1627868613281)(E:\飞秋\feiq\Recv Files\1\2.0 语言基础(已做笔记)\Pictures\关系运算符.PNG)] |
逻辑运算符
逻辑运算符:两个boolean类型的操作数或表达式进行逻辑比较。
| |
| :----------------------------------------------------------: |
| [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GsSUSgg3-1627868613282)(E:\飞秋\feiq\Recv Files\1\2.0 语言基础(已做笔记)\Pictures\逻辑运算符.PNG)] |
三元运算符(不熟)
类型转换
进行算数运算时:
两个操作数有一个为double,计算结果提升为double。
如果操作数中没有double,有一个为float,计算结果提升为float。
如果操作数中没有float,有一个为long,计算结果提升为long。
如果操作数中没有long,有一个为int,计算结果提升为int。
如果操作数中没有int,均为short或byte,计算结果仍旧提升为int。
注意:任何类型与String相加(+)时,实为拼接,其结果自动提升为String。
修饰符
访问权限修饰符
private
default
protected
-
保护型:如果一个类被protected修饰,那么这个类只能被自己的子类访问,无论子类在不在这个类所在的的包中
-
子类与基类在同一包中:被声明为 protected 的变量、方法和构造器能被同一个包中的任何其他类访问;
-
子类与基类不在同一包中:那么在子类中,子类实例可以访问其从基类继承而来的 protected 方法,而不能访问基类实例的protected方法。
public
一表看
修饰符 | 当前类 | 同一包内 | 子孙类(同一包) | 子孙类(不同包) | 其他包 |
---|---|---|---|---|---|
public | Y | Y | Y | Y | Y |
protected | Y | Y | Y | Y/N(说明) | N |
default | Y | Y | Y | N | N |
private | Y | N | N | N | N |
非访问修饰符
static
static成员变量
-
static 关键字用来声明独立于对象的静态变量,无论一个类实例化多少对象,它的静态变量只有一份拷贝。 静态变量也被称为类变量。局部变量不能被声明为 static 变量。
-
一个成员变量可用 static 关键字来修饰,这样的成员变量称为 static 成员变量,或静态成员变量。而没有用 static 关键字修饰的成员变量称为非静态成员变量。
-
只要静态成员变量所在的类被加载,这个静态成员变量就会被分配内存空间。因此在引用该静态成员变量时,通常不需要生成该类的对象,而是通过类名直接引用。
-
引用的方法是“类名 . 静态变量名”。当然仍然可以通过“对象名 . 静态变量名”的方式引用该静态成员变量。
-
相对应的非静态成员变量则属于对象而非类,只有在内存中构建该类对象时,非静态成员变量才被分配内存空间。
public class Student {
// 静态成员变量
private static String SchoolName;
private static int nums;
// 非静态成员变量
private String name;
private int age;
}
static成员方法
-
静态成员方法中不能使用 this、super 关键字,也不能调用非静态成员方法,同时不能引用非静态成员变量。
-
这个道理是显而易见的,因为静态成员方法属于类而不属于某个对象,而 this、super 都是对象的引用,非静态成员方法和成员变量也都属于对象。
package com.company;
public class Cs {
public static void main(String[] args) {
Dog dog1 = new Dog();
System.out.println("开始!");
//可以通过创造的对象调用方法
dog1.method();
//可以直接通过类名.方法名调用
Dog.method();
}
}
//定义一个父类Dog
class Animal{
private String action;
int age;
private int weigh;
public static void method(){
Dog.hei = 5;
System.out.println(Dog.hei);
System.out.println("调用了父类方法!");
}
}
//继承Animal类
class Dog extends Animal{
int weigh;
static int hei;
public static void method(){
System.out.println("调用了子类方法!");
Animal.method();
//Animal.getAction();无法从 static 上下文引用非 static 方法 'getAction()'
}
}
static代码块
package com.company;
public class Cs {
public static void main(String[] args) {
Dog dog1 = new Dog();
System.out.println("开始!");
dog1.age = 10;//继承父类Animal的变量
dog1.weigh = 15;
dog1.setAction("肉");//继承父类的方法
System.out.println(dog1.getAction());
dog1.method();
Dog.method();
}
}
//定义一个父类Dog
class Animal{
private String action;
int age;
private int weigh;
//定义一个方法
public void setAction(String action){//进行赋值
//添加逻辑判断,保证数据可取
if (action.equals("饭")){
this.action = action;
}else {
this.action = "菜";
}
}
public String getAction(){//返回返回值实现外部取值
return this.action;
}
public static void method(){
Dog.hei = 5;
System.out.println(Dog.hei);
System.out.println("调用了父类方法!");
}
}
//继承Animal类
class Dog extends Animal{
int weigh;
static int hei;
public Dog(){
System.out.println("构造方法!");
}
public static void method(){
System.out.println("调用了子类方法!");
Animal.method();
//Animal.getAction();无法从 static 上下文引用非 static 方法 'getAction()'
}
static {//优先输出,无论位置,当对象被创建时,就会直接输出,优先级高于构造方法
//Dog.weigh = 15;无法从 static 上下文引用非 static 字段 'weigh'
Dog.hei = 0;
System.out.println(hei);
System.out.println("静态代码块!");
}
}
static内部类
-
静态成员内部类的特点主要是它本身是类相关的内部类,所以它可以不依赖于外部类实例而被实例化。静态内部类不能访问其外部类的实例成员(包括普通的成员变量和方法),只能访问外部类的类成员(包括静态成员变量和静态方法)。即使是静态内部类的实例方法(非静态成员方法)也不能访问其外部类的实例成员。
final
final变量
- final 表示"最后的、最终的"含义,变量一旦赋值后,不能被重新赋值。被 final 修饰的实例变量必须显式指定初始值。final 修饰符通常和 static 修饰符一起使用来创建类常量。
- 虽然final无法被再次赋值但是可以使用变量进行运算
public class Cs {
static final int a = 15;
final int b = 20;
public static void main(String[] args) {
// a = 10;报错
System.out.println(a*10);
}
}
final方法
- 父类中的 final 方法可以被子类继承,但是不能被子类重写。声明 final 方法的主要目的是防止该方法的内容被修改。如下所示,使用 final 修饰符声明方法。
public class Test{
public final void changeName(){
// 方法体
}
}
final类
选择结构
定义:通过逻辑判断用户的选择进行操作
有关if
if语句
//用法
if(判断体){
执行的操作
}
if…else if…else语句
if(判断体){
执行的操作
}
else if(判断体){
执行的操作
continue;
}else{
执行的操作
}
if嵌套
if(判断体){
if(判断体){
if(判断体){
执行的操作
}
else if(判断体){
执行的操作
}else{
执行的操作
}
}
else if(判断体){
执行的操作
}else{
执行的操作
}
}
else if(判断体){
执行的操作
}else{
执行的操作
}
分支结构
语法:
switch(变量 | 表达式){
case 值1:
逻辑代码1;break;
case 值2:
逻辑代码2;break;
case 值n:
逻辑代码n;break;
default:
未满足时的逻辑代码;
}
可判断的类型:
- byte、short、int、char、String(JDK7+)
执行流程:
- 如果变量中的值等于值1,则执行逻辑代码1。
- 如果变量中的值等于值2,则执行逻辑代码2。
- 如果变量中的值等于值n,则执行逻辑代码n。
- 如果变量中的值没有匹配的case值时,执行default中的逻辑代码。
注意:当匹配的case执行后,不会自动退出整个结构,而是继续向下执行。
break关键字可在匹配的case执行后,跳出整个结构。
适用等值判断。
循环结构
while
可以当做for循环和if判断语句的结合体
判断括号中的布尔表达是否为真,为真则进行循环
while(判断体--布尔表达式){
循环体
}
//示例
int i=0;
while(i<=100){
System.out.println("wjs");//输出100次wjs
i++;//每循环一次i加一,否则会导致程序一直循环,i不满足结束条件
}
do{}while()
先执行一次循环体,再判断表达式是否为真,为真再次循环,为假退出循环
用于至少执行一次的程序
do{
循环体
}while(布尔表达式);
//示例
int i=0;
do{
System.out.println("wjs");//输出五次wjs
i++;
}while(i<5);
for循环
多用于遍历数组;
和while相同,先判断判断体为真或假;
适用于循环条件明确,数组的长度,或用户自主输入的长度。
for(int i=0;i<10(判断体);i++){
//执行操作
}
//示例
for(int i=0;i<10;i++){
System.out.println("执行第"+i+"次循环");
}
流程控制
break
直接结束最外层循环
continue
跳过本次循环,不执行操作
return
结束程序
方法
定义
实现特定功能的一段代码,可以被同一个类内的其他方法调用
用法
与main函数同级
package com.company;
public class Work6 {
public static int wjs(){
}
public static void main(String[] args) {
int i = 1;
wjs();//调用wjs
wrh(i)//调用wrh
}
public static int wrh(int i){
}
}
/*
方法类型就是数据类型,或为void,
如果为void则不用return返回数据类型
如果为数据类型,则在最后一行return返回数据类型
方法名必须小写,如果与其他方法同名,则形式参数类型不同或数量不同(重载)
public static 方法类型 方法名(){
}
*/
方法参数
实际参数
调用方法时传输的值为实参
package com.company;
public class Work6 {
public static int wjs(){
}
public static void main(String[] args) {
int i = 1;
wjs();//调用wjs
wrh(i)//调用wrh,i为实参
}
public static int wrh(int i){
}
}
可变长参数(int… a)
-
当传输的值想存进数组中再通过方法执行时可用
public static void main(String[] args) { score(12,15,32,21,15); } public static void score(int... a){//int... a <==> int[] a for (int i=0;i<a.length;i++){ System.out.print(a[i]+" "); } }
-
只适用于方法的形参只有一个
形式参数
定义方法时,在方法名后面的括号内的定义的数据类型或者数组类型
形参数量>=0
形参<==>局部变量声明
形参用于接收其他方法调用时传输过来的值。
public static 方法类型 方法名(形参){
}
递归
方法内部一直调用自己的方法就实现了递归
//斐波那契数列方法
public static int fibonacci(int n) {//n决定了递归几次
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);//返回所有递归结果的和
}
如果没有结束条件,则无穷递归
public static int fibonacci() {
System.out.printf("=")
fibonacci();//没有结束条件,就会无穷输出“=”
}
数组
一维数组
在用户需要存储大量相同类型数据时使用
语法
- 声明并定义长度 int[] a = new int[10];
- 声明并赋值 String[] a={“帝王蟹”,“霸王蟹”,“石斑鱼”,“羊肉串”,“牛肉串”,“烤全羊”,“白斩鸡”,“柠檬鸭”};
示例
声明
System.out.printf("请输入你要录入的学生数");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
int[] a = new int[i];//用户决定数组长度
利用循环一直赋值
System.out.printf("请输入你要录入的学生数");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
int[] a = new int[i];//用户决定数组长度
for(int j=0;j<a.length;j++){
Sys.o.pri("第"+i+"个学生:")
int score = input.nextInt();
a[i] = score;
}
一维数组的排序
冒泡排序
for (int i = 0; i < x.length - 1; i++) {//按顺序取出数组最后一位数以外的所有数
for (int j = 0; j < x.length - 1 - i; j++) {
if (x[j] < x[j + 1]) {//对比j以后的每一个值
int temp = x[j];//如果j>j+1,就要转换位置
x[j] = x[j + 1];
x[j + 1] = temp;
}
}
}
选择排序
for (int i = 0; i < x.length - 1; i++) {
for (int j = i + 1; j < x.length; j++) {//对比自己后面的所有数,如果大于后面的数就进行位置转换
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
二维数组
要录入的信息不止一种类型,例如:需要录入的信息有姓名、性别…
语法
String[][] f = new String[2][4];
f[0][0] = "ID";
f[0][1] = "用户名";
f[0][2] = "密码";
f[0][3] = "邮箱";
f[1][0] = "1";
f[1][1] = "wjs";
f[1][2] = "123";
f[1][3] = "123@qq.com";
数组的遍历
-
通过for循环一直输出数组内的值
//一维数组的遍历 int[] a = new int[10]; for(int i = 0;i<a.length;i++){ Sys.o.pri(a[i]+" "); } //二维数组 int[][] b = new int[10][10]; for(int i =0;i<b.length;i++){ for(int j=0;j<b[i].length;j++){ Sys.o.pri(b[i][j]+" "); } }
面对对象
概念
- 一切客观存在的事务都是对象,每个对象都有不同的特征、行为
- 对象:有属性、有方法
- 例如:计算器是一个对象,用户可以输出两个不同的数,不同的数可以选择不同的计算方法
- 一个类可以当作一个对象,类名就是对象的名字,类的实例变量就是其属性特征,类中的实例方法就是其行为
声明
public class TestConstructor {
public static void main(String[] args) {
Calculator s1 = new Calculator();//创造对象
float a = 15;
float b = 15;
s1.calculatorMath(a,b);//调用对象中的方法
s1.calculatorReduce(a,b);
s1.calculatorRide(a,b);
s1.calculatorExcept(a,b);
s1.calculatorMoreThan(a,b);
}
}
class Calculator{//计算器类,计算两个数的操作,对象
float userMath;//第一个数,实例变量,属性
float userMath1;//第二个数
//加减乘除余,行为方法
public static float calculatorMath(float a,float b){
float c = a+b;
return c;
}
public static void calculatorReduce(float a,float b){
float c = a-b;
System.out.println(a+"-"+b+"="+c);
}
public static void calculatorRide(float a,float b){
float c = a*b;
System.out.println(a+"*"+b+"="+c);
}
public static void calculatorExcept(float a,float b){
float c = a/b;
System.out.println(a+"/"+b+"="+c);
}
public static void calculatorMoreThan(float a,float b){
float c = a%b;
System.out.println(a+"%"+b+"="+c);
}
}
方法重载
概念
- 有很多种“吃”法,吃不同的东西有不同的方法,但是都是吃。
- 所以就有了方法重载
声明
- 方法名一样,但是方法的形参数或形参类型不同
- 就是吃不同的东西就有不同的动作,但是都是吃,方法名都为“eat”
class people{
public static void eat(){
Sys.o.pri("咀嚼");
}
public static void eat(int i){
Sys.o.pri("直接吞");
}
public static void eat(int i,String j){
Sys.o.pri("喝");
}
public static void eat(String i){
Sys.o.pri("咬");
}
}
构造方法
概念
- 方法名与类名一样的方法叫做构造方法
- 没有返回类型
- 创造对象的同时,构造方法被调用
- 如果没有定义方法,则默认创造对象的同时不作为
- 构造方法不可被继承
声明并访问
public class Cs {
public static void main(String[] args) {4
new Test();//可以直接new调用
Test te = new Test();//通过新建对象时调用
}
}
class Test{
//构造方法
public Test(){
System.out.println("hello word!");
}
}
构造方法的重载
概念
- 和方法重载一样
声明并访问
public class Cs {
public static void main(String[] args) {
new Test();
new Test(5);
Test te = new Test();
}
}
class Test{
//构造方法
public Test(){
System.out.println("hello word!");
}
public Test(int i){
System.out.println("hello word!"+i);
}
}
/*输出的结果:
hello word!
hello word!5
hello word!
新的对象te默认访问Test()方法
/
属性赋值
- 可以给类的实例变量赋值
语句
public class Cs {
public static void main(String[] args) {
new Test();
new Test(5);
Test te = new Test();
Test tes = new Test(4);
System.out.println("==="+tes.chose+"===");
}
}
class Test{
int chose;
//构造方法
public Test(){
System.out.println("hello word!");
}
public Test(int i){
chose = i+5;
System.out.println("hello word!"+i+" "+chose);
}
}
/*
hello word!
hello word!5 10
hello word!
hello word!4 9
===9===
*/
this关键字
调用实例属性(变量)
- 当实例方法内的局部变量名与实例类中的实例变量名一样时使用
- 如果需要调用的是实例变量,则在变量名前加上"this."
- 如果没有加上,则默认调用的是实例方法类的局部变量
public class Cs {
public static void main(String[] args) {
new Test();
new Test(5);
Test te = new Test();
Test tes = new Test(4);
System.out.println("==="+tes.chose+"===");
}
}
class Test{
int chose = 1;
//构造方法
public Test(){
System.out.println("hello word!");
}
public Test(int i){
int chose = i+5;
System.out.println("hello word!"+i+" "+chose);//更改的是方法内局部变量
System.out.println(this.chose);//访问的是实例方法
}
}
/*
hello word!
hello word!5 10
1
hello word!
hello word!4 9
1
===1===
/
调用构造方法
- 只能构造方法被重载时使用
- 必须在重载的构造方法的第一行,且只能唯一
- 不能对自己使用,只能对其他重载构造方法使用
- 不能递归使用,不能进行循环调用
public class Cs {
public static void main(String[] args) {
new Test();
new Test(5);
Test tes = new Test(4);
System.out.println("==="+tes.chose+"===");
}
}
class Test{
int chose = 1;
//构造方法
public Test(){
this("s");
System.out.println("hello word!");
//this(5);此位置不行,只能在构造方法的第一行
}
public Test(int i){
this();//使用Test()
int chose = i+5;
System.out.println("hello word!"+i+" "+chose);//更改的是方法内局部变量
System.out.println(this.chose);//访问的是实例方法
}
public Test(String i){
// this();被警告递归使用
// this(2);被警告递归使用
String chose = i+5;
System.out.println(chose);//更改的是方法内局部变量
}
}
/*
s5
hello word!
s5
hello word!
hello word!5 10
1
s5
hello word!
hello word!4 9
1
===1===
/
三大特性
封装
概念
- 为了防止用户输入不合法数据赋值给实例变量导致的一系列问题
- 用户不可以直接对实例变量进行使用,必须通过实例方法使用
示例
package com.company;
public class Cs {
public static void main(String[] args) {
Animal dog = new Animal();
//dog.action = “吃饭”;实例变量action已经修饰符private(个人)修饰,不能直接用
dog.setAction("肉");
System.out.println(dog.getAction());
dog.setAction("饭");
System.out.println(dog.getAction());
}
}
//定义一个类Animal
class Animal{
private String action;
//定义一个方法
public void setAction(String action){//进行赋值
//添加逻辑判断,保证数据可取
if (action.equals("饭")){
this.action = action;
}else {
this.action = "菜";
}
}
public String getAction(){//返回返回值实现外部取值
return this.action;
}
}
继承
概念
-
继承即父子关系
-
子类可以继承和访问父类中的所有方法和变量(被private修饰的变量不可以访问继承)
-
父类就是所有子类都有的共同点,但是不同子类都有自己独特的属性或方法(可以重写父类方法)
-
同包内的其他类可以通过子类继承调用父类
-
不同包的其他类只可以访问继承被public修饰的类
特点
- 一个子类只能有一个父类
- 一个父类可以有多个子类
- 可以层层逐级叠加
- 就像家庭关系一样
示例
package com.company;
public class Cs {
public static void main(String[] args) {
//dog1.weigh = 150;被private修饰的变量子类也不可以继承
dog1.age = 10;//继承父类Animal的变量
dog1.weigh = 15;
dog1.setAction("肉");//继承父类的方法
System.out.println(dog1.getAction());
}
}
//定义一个父类Dog
class Animal{
private String action;
int age;
private int weigh;
//定义一个方法
public void setAction(String action){//进行赋值
//添加逻辑判断,保证数据可取
if (action.equals("饭")){
this.action = action;
}else {
this.action = "菜";
}
}
public String getAction(){//返回返回值实现外部取值
return this.action;
}
}
//继承Animal类
class Dog extends Animal{
int weigh;
}
方法重写
- 可以在子类中进行方法重写,覆盖掉父类中的方法
- 优先执行重写后的方法
package com.company;
public class Cs {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.age = 10;//继承父类Animal的变量
dog1.weigh = 15;
dog1.setAction("肉");//继承父类的方法
System.out.println(dog1.getAction());
dog1.method();
Dog.method();
}
}
//定义一个父类Dog
class Animal{
private String action;
int age;
private int weigh;
//定义一个方法
public static void method(){
Dog.hei = 5;
System.out.println(Dog.hei);
System.out.println("调用了父类方法!");
}
}
//继承Animal类
class Dog extends Animal{
int weigh;
static int hei;
public static void method(){
System.out.println("调用了子类方法!");
Animal.method();
}
}
/*
菜
调用了子类方法!
5
调用了父类方法!/
super关键字
package com.company;
public class Cs {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.method();
}
}
//定义一个父类Dog
class Animal{
private String action;
int age = 15;
private int weigh;
public String getAction(){//返回返回值实现外部取值
return this.action;
}
public void method(){
Dog.hei = 5;
System.out.println(Dog.hei);
System.out.println("调用了父类方法!");
}
}
//继承Animal类
class Dog extends Animal{
int weigh;
int age = 25;
static int hei;
public void method(){
int age = 20;
System.out.println("调用了子类方法!");
System.out.println(age);//局部变量,方法内的age
System.out.println(this.age);//实例变量,类中的age
System.out.println(super.age);//父类实例变量,父类中的age
super.method();
}
}
继承中的对象创建
- 创造一个子类对象时,同时构建了其父类
public class Cs {
public static void main(String[] args) {
C c = new C();;
}
}
class A{
String a = "爷爷在此!";
public A(){
System.out.println("A - - 被构造 - - "+a);
}
}
class B extends A{
String b = "爸爸在此!";
public B(){
System.out.println("B - - 被构造 - - "+b);
}
public B(int i){
System.out.println("Bi - - 被构造 - - "+b);
}
}
class C extends B{
String c = "儿子在此!";
public C(){
super();//表示调用父类无参构造方法。如果没有显示书写,隐式存在于子类构造方法的首行。
System.out.println("C - - 被构造 - - "+c);
}
public C(int i){
super(i);//表示调用父类有参构造方法。如果没有显示书写,隐式存在于子类构造方法的首行。
System.out.println("Ci - - 被构造 - - "+c);
}
}
/*
A - - 被构造 - - 爷爷在此!
B - - 被构造 - - 爸爸在此!
C - - 被构造 - - 儿子在此!
/
多态
概念:父类引用指向子类对象,从而产生多种形态。
Animal a = new Dog();
二者具有直接或间接的继承关系时,父类引用可指向子类对象,即形成多态。
父类引用仅可调用父类所声明的属性和方法,不可调用子类独有的属性和方法。
tic int hei;
public void method(){
int age = 20;
System.out.println(“调用了子类方法!”);
System.out.println(age);//局部变量,方法内的age
System.out.println(this.age);//实例变量,类中的age
System.out.println(super.age);//父类实例变量,父类中的age
super.method();
}
}
#### 继承中的对象创建
- 创造一个子类对象时,同时构建了其父类
```java
public class Cs {
public static void main(String[] args) {
C c = new C();;
}
}
class A{
String a = "爷爷在此!";
public A(){
System.out.println("A - - 被构造 - - "+a);
}
}
class B extends A{
String b = "爸爸在此!";
public B(){
System.out.println("B - - 被构造 - - "+b);
}
public B(int i){
System.out.println("Bi - - 被构造 - - "+b);
}
}
class C extends B{
String c = "儿子在此!";
public C(){
super();//表示调用父类无参构造方法。如果没有显示书写,隐式存在于子类构造方法的首行。
System.out.println("C - - 被构造 - - "+c);
}
public C(int i){
super(i);//表示调用父类有参构造方法。如果没有显示书写,隐式存在于子类构造方法的首行。
System.out.println("Ci - - 被构造 - - "+c);
}
}
/*
A - - 被构造 - - 爷爷在此!
B - - 被构造 - - 爸爸在此!
C - - 被构造 - - 儿子在此!
/
多态
概念:父类引用指向子类对象,从而产生多种形态。
Animal a = new Dog();
二者具有直接或间接的继承关系时,父类引用可指向子类对象,即形成多态。
父类引用仅可调用父类所声明的属性和方法,不可调用子类独有的属性和方法。