一些范例
0.0
public class qwer{
public static void main(String[] args){
Person p1 = new Person();
}
}
}
class Person{
}
class Person{
public Person{
}
}
class Person{
int age;
public Person(int a){
}
}
基础
1.0
public class qwer {
public static void main(String[] args) {
Person p2 = new Person();
Person p1 = new Person();
p1.setAge(30);
p1.setName("Zhangsan");
p2.setAge(-80);
p2.setName("lisi");
p1.introudce();
p2.introudce();
}
}
class Person{
private int age;
private String name;
public String getName(){
return name;
}
public void setName(String perName) {
name = perName;
}
public int getAge(int perAge) {
return age;
}
public void setAge(int perAge) {
if(perAge<0)
System.out.println("null");
else
age=perAge;
}
public void introudce(){
System.out.println("I am "+name+".");
System.out.println("I am "+age+"years old.");
}
}
1.1
public class qwer {
public static void main(String[] args) {
Person p2 = new Person(55);
p2.speak();
}
}
class Person{
int age;
public Person(int a){
age=a;
}
public void speak(){
System.out.println("I am "+age+" years old.");
}
}
1.2
public class qwer {
public static void main(String[] args) {
Person p1 = new Person("张三",18);
Person p2 = new Person("王五");
p1.speak();
p2.speak();
}
}
class Person{
String name;
int age;
public Person(String con_name,int con_age){
name=con_name;
age=con_age;
}
public Person(String con_name){
name=con_name;
}
public void speak(){
System.out.println("我是"+name+",我今年"+age+"岁。");
}
}
1.3
public class qwer {
public static void main(String[] args) {
Person p = new Person("qqqqqqqqq");
}
}
class Person{
public Person(){
System.out.println("aaaaaaaaa");
}
public Person(String name){
this();
System.out.println("bbbbbbbbb");
}
}
1.4 静态内部类
public class qwer {
public static void main(String[] args) {
Person.Inner inner = new Person.Inner();
inner.show();
}
}
class Person{
private static int num=6;
static class Inner{
void show(){
System.out.println("num="+num);
}
}
}
1.5 方法内部类
public class qwer {
public static void main(String[] args){
Outer outer = new Outer();
outer.test();
}
}
class Outer{
private int a=4;
public void test(){
class Inner{
void show(){
System.out.println(a);
}
}
Inner in = new Inner();
in.show();
}
}
继承
2.1 继承
public class qwer {
public static void main(String[] args){
Dog dog = new Dog();
dog.name="沙皮狗";
dog.printName();
dog.shout();
}
}
class Animal{
String name;
void shout(){
System.out.println("汪汪汪");
}
}
class Dog extends Animal{
public void printName(){
System.out.println(name);
}
}
2.2 类的继承
public class qwer {
public static void main(String[] args){
Dog dog = new Dog();
dog.name="沙皮狗";
dog.printName();
dog.shout();
}
}
class Animal{
String name;
void shout(){
System.out.println("汪汪汪");
}
}
class Dog extends Animal{
public void printName(){
System.out.println(name);
}
}
2.3 多个子类可以继承于一个父类
public class qwer {
public static void main(String[] args){
Dog dog = new Dog();
Cat cat = new Cat();
dog.shout();
dog.name="沙皮狗";
dog.dogName();
dog.dogMove();
cat.shout();
cat.catName();
cat.catMove();
}
}
class Animal{
String name;
void shout(){
System.out.println("有一只");
}
}
class Dog extends Animal{
public void dogName(){
System.out.println(name);
}
public void dogMove(){
System.out.println("快快的跑");
}
}
class Cat extends Animal{
public void catName(){
System.out.println("加菲猫");
}
public void catMove(){
System.out.println("蹬蹬的跑");
}
}
2.4 super关键字
public class qwer {
public static void main(String[] args){
Dog dog = new Dog();
dog.shout();
dog.printNmae();
}
}
class Animal{
String name="动物";
void shout(){
System.out.println("你是一只");
}
}
class Dog extends Animal{
String name="沙皮狗";
void shout() {
super.shout();
}
void printNmae(){
System.out.println(super.name);
}
}
2.5 final 关键字
public class qwer {
public static void main(String[] args){
final int a=6;
a=10;
}
抽象、接口
3.1 抽象类
public class qwer {
public static void main(String[] args){
Dog dog = new Dog();
dog.shout();
}
}
abstract class Animal{
abstract void shout();
}
class Dog extends Animal{
void shout(){
System.out.println("汪汪汪……");
}
}
3.2 接口
public class qwer {
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.run();
}
}
interface Animal{
int ID=1;
void breathe();
void run();
}
class Dog implements Animal{
public void breathe(){
System.out.println("狗崽在呼吸");
}
public void run(){
System.out.println("狗崽在狂奔");
}
}
3.3 接口继承
public class qwer {
public static void main(String[] args){
Dog dog = new Dog();
dog.Move();
dog.breathe();
dog.run();
}
}
interface Animal{
int ID=1;
void breathe();
void run();
}
interface Quanke extends Animal{
void Move();
}
class Dog implements Quanke{
public void breathe(){
System.out.println("狗崽在呼吸");
}
public void run(){
System.out.println("狗崽在狂奔");
}
public void Move() {
System.out.println("狗崽活着");
}
}
3.4 多态
public class qwer {
public static void main(String[] args){
Animal an1 = new Cat();
Animal an2 = new Dog();
Animalshout(an1);
Animalshout(an2);
}
public static void Animalshout(Animal an){
an.shout();
}
}
interface Animal{
void shout();
}
class Cat implements Animal{
public void shout(){
System.out.println("喵喵……");
}
}
class Dog implements Animal{
public void shout(){
System.out.println("汪汪……");
}
}
|