public class HelloWorld {
static int score1 = 86;
static int score2 = 92;
public static int sum() {
int sum1=score1+score2;
return sum1;
}
public static void main(String[] args) {
int allScore = sum();
System.out.println("总分:" + allScore);
}
}
static int score1 = 86;
static int score2 = 92;
public static int sum() {
int sum1=score1+score2;
return sum1;
}
public static void main(String[] args) {
int allScore = sum();
System.out.println("总分:" + allScore);
}
}
2017-06-08
public class Text {
private float screen;
public Text(){
System.out.println("无参的构造方法执行了!");
}
public Text(float newScreen){
if(newScreen<3.5f){
screen=3.5f;
System.out.println("有误,赋值为默认值3.5");
}
else{
screen=newScreen;
}
System.out.println("有参的构造方法执行了!");
System.out.println(screen);
}
}
private float screen;
public Text(){
System.out.println("无参的构造方法执行了!");
}
public Text(float newScreen){
if(newScreen<3.5f){
screen=3.5f;
System.out.println("有误,赋值为默认值3.5");
}
else{
screen=newScreen;
}
System.out.println("有参的构造方法执行了!");
System.out.println(screen);
}
}
2017-06-07
public class Text {
float screen;
float cpu;
float mem;
int var=30;
void call(){
int localVar=10;
int var=40;
System.out.println("localVar:"+localVar);
System.out.println("var:"+var);
System.out.println("Telphone有打电话的功能!");
}
}
float screen;
float cpu;
float mem;
int var=30;
void call(){
int localVar=10;
int var=40;
System.out.println("localVar:"+localVar);
System.out.println("var:"+var);
System.out.println("Telphone有打电话的功能!");
}
}
2017-06-07
抽象方法没有方法体,以分号结束
package imooc.com;
public abstract class Shape {
int x=2,y=3;
int r=3;
float pi=3.1415f;
public abstract void Perimeter();
public abstract void Area();
}
package imooc.com;
public abstract class Shape {
int x=2,y=3;
int r=3;
float pi=3.1415f;
public abstract void Perimeter();
public abstract void Area();
}
2017-06-07
package imooc.com;
public class InitialShape {
public static void main(String Args[]){
Shape shape1=new Rectangle();
shape1.Perimeter(); //使用抽象方法
shape1.Area();
Shape shape2=new Circle(); //利用多态
shape2.Perimeter();
shape2.Area();
}
}
public class InitialShape {
public static void main(String Args[]){
Shape shape1=new Rectangle();
shape1.Perimeter(); //使用抽象方法
shape1.Area();
Shape shape2=new Circle(); //利用多态
shape2.Perimeter();
shape2.Area();
}
}
2017-06-07
package imooc.com;
public class Circle extends Shape {
@Override
public void Perimeter() {
System.out.println("circle`s Perimeter is : "+pi*r*2);
}
@Override
public void Area() {
// TODO Auto-generated method stub
System.out.println("circle`s Area is : "+pi*r*r);
}
}
public class Circle extends Shape {
@Override
public void Perimeter() {
System.out.println("circle`s Perimeter is : "+pi*r*2);
}
@Override
public void Area() {
// TODO Auto-generated method stub
System.out.println("circle`s Area is : "+pi*r*r);
}
}
2017-06-07
package imooc.com;
public class Rectangle extends Shape {
public void Perimeter() {
System.out.println("Rectangle`s Perimeter is : "+(x+y)*2);
}
public void Area() {
System.out.println("Rectangle`s Area is : "+x*y);
}
}
public class Rectangle extends Shape {
public void Perimeter() {
System.out.println("Rectangle`s Perimeter is : "+(x+y)*2);
}
public void Area() {
System.out.println("Rectangle`s Area is : "+x*y);
}
}
2017-06-07
package imooc.com;
public abstract class Shape {
int x=2,y=3;
int r=3;
float pi=3.1415f;
public void Perimeter(){
};
public void Area(){
};
}
public abstract class Shape {
int x=2,y=3;
int r=3;
float pi=3.1415f;
public void Perimeter(){
};
public void Area(){
};
}
2017-06-07
使用final关键字做标识有最终的含义
final可以修饰类、方法、属性和变量
final修饰类时,则该类不允许被继承
final修饰方法时,则该方法不允许被覆盖(重写)
final修饰属性时,则属性不会进行隐式的初始化(即在声明属性的同时要对它进行初始化,或在构造方法中进行赋值,二者只能选其一)
final修饰变量时,则该变量的值只能赋值一次,则变为常量
final可以修饰类、方法、属性和变量
final修饰类时,则该类不允许被继承
final修饰方法时,则该方法不允许被覆盖(重写)
final修饰属性时,则属性不会进行隐式的初始化(即在声明属性的同时要对它进行初始化,或在构造方法中进行赋值,二者只能选其一)
final修饰变量时,则该变量的值只能赋值一次,则变为常量
2017-06-07
因为在方法里局部变量是没有初始值的,有参的构造方法的好处是给方法里的局部变量赋值,这时候构造方法里的参数的值就等于成员变量的值。有了初始值。
2017-06-06