static
string 赋值相等
内存
定义数组
int[] a = new int [5];
java 第三次课
继承:
overload
overread
作用:
代码重用
抽象方法`
abstract Class Person{
private string name;
pubilc Person(String name){
this.name = name;
}
public String name getName(){
return name;
}
public void sayHello(){
System.out.println("");
}
/*
public abstract void sayHello(); 抽象方法 抽象类 不能实例化 即不能new person了
*/
}
class Student extends Person {
private string major;
public Student(String major){
this.major = major;
}
/*
super(name);
this(major);
*/
public Studen(String name,String major){
super(name);
this.major=major;
}
public void sayHello(){
System.out.println("I am " + this.getName() + ", from "+ major);
}
}
public class Hello {
public static void main(String[] args){
Student s =new Student("Alice","SERVANT_RETENTION_POLICY_ID");
s.sayHello();
/* Person p=new Person();
Student s=new Student();
p=s;
s=p;
*/
Person p=new Student("Alice","SE");
p.sayHello();
}
}
程序实现逻辑
1.父类与子类有共有的字段或方法
2.父类将方法设置为抽象方法,抽象方法相当于c/c++里面的纯虚函数
3.this 与 super 不能同时在一个构造方法里,因为都需要在第一行
编译逻辑:
栈上是person 堆上是student
先编译,再运行,在编译中,父类有此方法,所以编译通过,而在运行中用的是student的sayhello
#include<cstdio>
#include<cstdlib>
//using namespace std;
int *p(int ,int );
int add(int a,int b){
return a+b;
}
int main()
{
p=add;
int a = (*p)(2,4);
printf()
}
函数指针
#include<cstdio>
#include<cstdlib>
//using namespace std;
typedef int (*p) (int a,int b);
int hello(p f,int a,int b){
return f(a,b);
}
int add(int a,int b){
return a+b;
}
int main()
{
int s=hello(add.2,4);
printf("%d",s);
return 0;
}
继承
多态性
实例演示
先编写程序保存,然后编译为class文件,打包在另一个地方复制,然后使用。
类型转换
package cn.edu.hit;
import com.youqian.Person;
import com.youqian.PersonUtil;
public class Hello {
public static void main(String[] args){
PersonUtil pu=new PewsonUtil();
Student s = new Student("Alice","SE");
pu.hello;
Person p= new Student("Bob ", "Se ");
s = (Student) p;
}
}
new Student(“Alice”,“SE”);这样 p 实际上就已经返回 Student 类型
不加(Student) 的话没有办法编译通过,因为编译器不认识,所以要强转类型,然后编译通过,再运行。
单继承 多实现
接口 父类
泛型
class Employee implements Comparable object 类型 T类型
extends 和implements
继承的数量不同
class Student:Person,Eatable ,Runnable
c++ 后三个必须是类
c# 第一个可以是类可以是接口,后三个必须是接口
java
class Student extends Person implements Eatable,Runnable{
}
回调
进度条 UI 命令行程序