java面向对象(三)常量的定义&类的继承
2013-09-16 16:56
309 查看
1】常量的定义
使用关键字final定义常量,例如:
final double PI=3.1415926;
作为一种约定,在定义常量时,通常采用大写形式
final常量可以在声明的同时赋初始值,也可以在构造函数中赋初始值。
为了节省内存,我们通常将常量声明为静态的(static).
rihiroshitekiMacBook-Air:test LiBo$ cat Point.java
class Point
{
int x,y;
final double PI=3.1415926;
Point(int a,int b)
{
x=a;
y=b;
}
Point()
{
this(1,1);
}
static void output()
{
// System.out.println(x);
// System.out.println(y);
System.out.println("output() called");
}
void output(int x,int y)
{
this.x=x;
this.y=y;
}
public static void main(String[] args)
{
Point.output();
/* Point pt;
// pt=new Point(3,3);
pt=new Point();
// pt.output(5,5);
// pt.output();
Point pt2=new Point();
pt.x=6;
pt2.x=7;
System.out.println(pt.x);
System.out.println(pt2.x);*/
}
}
class Point
{
int x,y;
final double PI;//=3.1415926;
Point(int a,int b)
{
PI=3.1415926;
x=a;
y=b;
}
Point()
{
PI=3.1415926;
//this(1,1);
}
static void output()
{
// System.out.println(x);
// System.out.println(y);
System.out.println("output() called");
}
2】类的继承
理解继承是理解面向对象程序设计的关键
在java中,通过关键字extentds继承一个已有的类,被继承的类称为父类(超类,基类),新的类称为子类(派生类)
rihiroshitekiMacBook-Air:test LiBo$ cat Animal.java
class Animal
{
int height,weight;
void eat()
{
System.out.println("animal eat");
}
void sleep()
{
System.out.println("animal sleep");
}
void breathe()
{
System.out.println("animal breathe");
}
}
class Fish extends Animal
{
}
class Integration
{
public static void main(String[] args)
{
Animal an=new Animal();
Fish fh=new Fish();
an.breathe();
fh.height=30;
fh.breathe();
}
}
使用关键字final定义常量,例如:
final double PI=3.1415926;
作为一种约定,在定义常量时,通常采用大写形式
final常量可以在声明的同时赋初始值,也可以在构造函数中赋初始值。
为了节省内存,我们通常将常量声明为静态的(static).
rihiroshitekiMacBook-Air:test LiBo$ cat Point.java
class Point
{
int x,y;
final double PI=3.1415926;
Point(int a,int b)
{
x=a;
y=b;
}
Point()
{
this(1,1);
}
static void output()
{
// System.out.println(x);
// System.out.println(y);
System.out.println("output() called");
}
void output(int x,int y)
{
this.x=x;
this.y=y;
}
public static void main(String[] args)
{
Point.output();
/* Point pt;
// pt=new Point(3,3);
pt=new Point();
// pt.output(5,5);
// pt.output();
Point pt2=new Point();
pt.x=6;
pt2.x=7;
System.out.println(pt.x);
System.out.println(pt2.x);*/
}
}
class Point
{
int x,y;
final double PI;//=3.1415926;
Point(int a,int b)
{
PI=3.1415926;
x=a;
y=b;
}
Point()
{
PI=3.1415926;
//this(1,1);
}
static void output()
{
// System.out.println(x);
// System.out.println(y);
System.out.println("output() called");
}
2】类的继承
理解继承是理解面向对象程序设计的关键
在java中,通过关键字extentds继承一个已有的类,被继承的类称为父类(超类,基类),新的类称为子类(派生类)
rihiroshitekiMacBook-Air:test LiBo$ cat Animal.java
class Animal
{
int height,weight;
void eat()
{
System.out.println("animal eat");
}
void sleep()
{
System.out.println("animal sleep");
}
void breathe()
{
System.out.println("animal breathe");
}
}
class Fish extends Animal
{
}
class Integration
{
public static void main(String[] args)
{
Animal an=new Animal();
Fish fh=new Fish();
an.breathe();
fh.height=30;
fh.breathe();
}
}
相关文章推荐
- C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
- C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
- C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
- 黑马程序员_毕向东JAVA基础_面向对象(封装&继承&多态)
- Java基础<六>---> 面向对象与java特性(封装、继承、多态
- C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
- C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
- C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
- C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
- 关于 "java中常量定义在interface中好还是定义在class中好" 的一些思考
- 黑马程序员 JAVA基础<二> 面向对象之封装 继承 多态
- C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
- java学习路线__面向对象__继承的定义与使用__9
- Java学习笔记11 面向对象之继承与…
- java学习之旅40--面向对象_13_继承_Object类用法_toString和equals方法_重写toString
- Java面向对象——继承
- java:面向对象的三大特性之一:继承
- 黑马程序员_Java基础_面向对象(继承、子父类变量和函数以及构造函数特点、final关键字、抽象类、模版方法模式、接口)
- java学习之旅42--面向对象_15_继承_组合
- java面向对象的特性 封装 继承 多态