学习java第11天
1.学习内容
*变量分 基本类型 和 引用型。
public class MyDate {
private int day;
private int month;
private int year;
public MyDate(int y, int m, int d) {
year = y;
month = m;
day = d;
}
void addYear(){
year ++;
}
public void display() {
System.out.println(year + "-" + month + "-" +day);
}
public static void main(String[] args) {
MyDate m = new MyDate(2020, 7, 16);
MyDate n = m;
n.addYear();
m.display();
n.display();
}
}
当调试时显示的都是2021,因为引用的是同一个对象
(2)变量又分字段变量和局部变量
*字段变量为对象的一部分,存在于堆中,局部变量存在于栈中
字段变量可以自动赋初值,局部变量则必须显示赋值
(3)变量的传递
Java是值传递,也就是将表达式的值复制给形式参数。
而引用型变量,传递的值是引用值,不是复制对象实体
public class TransByValue {
public static void main (String[] args) {
int a = 0;
modify (a); System.out.println(a);//显示结果t:0
int a = 0;
modify (a); System.out.println(a);//显示结果t:0
int [] b = new int [1];
modify(b);
System.out.println(b[0]); //显示结果t:1
}
modify(b);
System.out.println(b[0]); //显示结果t:1
}
public static void modify (int a) {
a++;
}
public static void modify (int[] b) {
b[0] ++;
b = new int[5];
}
}
a++;
}
public static void modify (int[] b) {
b[0] ++;
b = new int[5];
}
}
(4)变量的返回
(5)多态
*上溯造型 把派生类当成基本类型处理
Person p = new Student();
void fun (Person p){......}
fun (new Person());
调用时用虚方法调用,可实现运行时的多态
class TestVirtualInvoke
{
static void doStuff( Shape s ){
s.draw();
}
{
static void doStuff( Shape s ){
s.draw();
}
public static void main( String [] args ){
Circle c = new Circle();
Triangle t = new Triangle();
Line l = new Line();
doStuff(c);
doStuff(t);
doStuff(l);
}
}
class Shape
{
void draw(){ System.out.println("Shape Drawing"); }
}
class Circle extends Shape
{
void draw(){ System.out.println("Draw Circle"); }
}
Circle c = new Circle();
Triangle t = new Triangle();
Line l = new Line();
doStuff(c);
doStuff(t);
doStuff(l);
}
}
class Shape
{
void draw(){ System.out.println("Shape Drawing"); }
}
class Circle extends Shape
{
void draw(){ System.out.println("Draw Circle"); }
}
class Triangle extends Shape
{
void draw(){ System.out.println("Draw Three Lines"); }
}
{
void draw(){ System.out.println("Draw Three Lines"); }
}
class Line extends Shape
{
void draw(){ System.out.println("Draw Line"); }
}
{
void draw(){ System.out.println("Draw Line"); }
}
*三种非虚的方法
static方法,以声明的类型为准,与实例类型无关
private方法,子类无法看见,也就不存在虚化问题
final方法,子类不能覆盖,不存在虚化问题
对比上一个代码:
class TestStaticInvoke
{
static void doStuff( Shape s ){
s.draw();
}
public static void main( String [] args ){
Circle c = new Circle();
Triangle t = new Triangle();
Line l = new Line();
doStuff(c);
doStuff(t);
doStuff(l);
Shape s = new Circle();
doStuff(s);
s.draw();
Circle c2 = new Circle();
c2.draw();
}
}
class Shape
{
static void draw(){ System.out.println("Shape Drawing"); }
}
class Circle extends Shape
{
static void draw(){ System.out.println("Draw Circle"); }
}
{
static void doStuff( Shape s ){
s.draw();
}
public static void main( String [] args ){
Circle c = new Circle();
Triangle t = new Triangle();
Line l = new Line();
doStuff(c);
doStuff(t);
doStuff(l);
Shape s = new Circle();
doStuff(s);
s.draw();
Circle c2 = new Circle();
c2.draw();
}
}
class Shape
{
static void draw(){ System.out.println("Shape Drawing"); }
}
class Circle extends Shape
{
static void draw(){ System.out.println("Draw Circle"); }
}
class Triangle extends Shape
{
static void draw(){ System.out.println("Draw Three Lines"); }
}
{
static void draw(){ System.out.println("Draw Three Lines"); }
}
class Line extends Shape
{
static void draw(){ System.out.println("Draw Line"); }
}
{
static void draw(){ System.out.println("Draw Line"); }
}
2.明天学习内容
对象构造与初始化
对象清除与垃圾回收