You are on page 1of 7

import java.util.

*;
import java.io.*;
class HelloWorld {
public static void main(String[] args) throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

String c;
System.out.println("Enter your name:");
c = br.readLine();
System.out.println("Hello "+c+"!Welcome to our planet Earth.");
}
}
----nhap ten hien thi-----

----------------------------------------------------------------

package oops;

public class TestMain {

public static void main(String[] args) {

Person john=new Person("101","John");


Person allice=new Person("102","Allice");
Person kathy=new Person("103","Kathy");

john.display();
allice.display();
kathy.display();

class Person{

String userId;
String name;

//constructor
Person(){
System.out.println("person class constructor...");
}

Person(String userId,String name){


System.out.println("person class parameterized constructor...");
this.userId=userId;
this.name=name;
}

public void display() {


System.out.println(userId+"\t\t"+name);
}
}
-------------nhap ten--------

----------------------------------------------------------------

package oops;

public class TestEncapsulation {

public static void main(String[] args) {

Student john=new Student(); // object john


String nameBefore= john.getName();
System.out.println(nameBefore);

john.setName("JOHN");
String nameAfter= john.getName();
System.out.println(nameAfter);

class Student{

private String name;

//getters-setters

public String getName() {


return name;
}

public void setName(String name) {


if(name.length()>5) {
this.name = name;
}

}
------------------nhập tên khai báo get/set---------------

package oops;

public class TestInheritance {

public static void main(String[] args) {

Persons persons=new Persons();


Persons user=new User();
Persons admin=new Admin();

persons.checkPerson();
user.checkPerson();
admin.checkPerson();
}

class Persons{

public void checkPerson() {


System.out.println("This belongs to Person....");
}
}

class User extends Persons{

@Override
public void checkPerson() {
System.out.println("This belongs to User....");
}
}

class Admin extends Persons{

@Override
public void checkPerson() {
System.out.println("This belongs to Admin....");
}
}
---------viết các lớp kế thừa----------

------------------------------------------

package oops;

public class TestStatic {

public static void main(String[] args) {

Customers c=new Customers();


System.out.println(c.name);

System.out.println(Customers.id);

c.test1();
Customers.test2();
}

class Customers{

String name;
static int id;

public void test1() {

public static void test2() {

}
}
------------------------------------static-------------

-------------------------------------------------------------------------

package oops;

public class TestInheritance {

public static void main(String[] args) {

Persons persons=new Persons();


Persons user=new User();
Persons admin=new Admin();

persons.checkPerson();
user.checkPerson();
admin.checkPerson();
}

//class Persons{
//
// public void checkPerson() {
// System.out.println("This belongs to Person....");
// }
//}
//
//class User extends Persons{
//
// @Override
// public void checkPerson() {
// System.out.println("This belongs to User....");
// }
//}
//
//class Admin extends Persons{
//
// @Override
// public void checkPerson() {
// System.out.println("This belongs to Admin....");
// }
//}

//class Object{
//
//}
//
//class Persons extends Object{
//
// public void checkPerson() {
// System.out.println("This belongs to Person....");
// }
//}
//
//class User extends Persons{
//
// @Override
// public void checkPerson() {
// System.out.println("This belongs to User....");
// }
//}
//
//class Admin extends User{
//
// @Override
// public void checkPerson() {
// System.out.println("This belongs to Admin....");
// }
//}

class Persons{

public void checkPerson() {


System.out.println("This belongs to Person....");
}
}

class User extends Persons{

@Override
public void checkPerson() {
System.out.println("This belongs to User....");
}
}

class Admin extends User,Persons{

@Override
public void checkPerson() {
System.out.println("This belongs to Admin....");
}
}
-----------------inheritance------------------

---------------------------------------------------------

package oops;

public class TestEncapsulation {

public static void main(String[] args) {

Student john=new Student(); // object john


String nameBefore= john.getName();
System.out.println(nameBefore);

john.setName("JOHN");
String nameAfter= john.getName();
System.out.println(nameAfter);

class Student{
private String name;

//getters-setters

public String getName() {


return name;
}

public void setName(String name) {


if(name.length()>5) {
this.name = name;
}

}
------------Encapsulation---------------------

----------------------------------------------------------
package oops;

public class TestOverloading {

public static void main(String[] args) {

//Maths m=new Maths();

String res1= Maths.add("welcome", "guys");


double res2= Maths.add(2.5, 2.5);

System.out.println(res1+"\t"+res2);
}

class Maths{

static int add(int a,int b) {


//System.out.println(a+b);
return a+b;
}

static String add(String s1,String s2) {


//System.out.println(s1+s2);
return s1+s2;
}

static double add(double d1,double d2) {


System.out.println
//System.out.println(d1+d2);
return d1+d2;
}
}
-------------phép toán------------

-----------------------------------------------
package oops;

public class InitializerTest {

{
System.out.println("instance/non-static initializer block");
}

static {
System.out.println("static initializer block");

public static void main(String[] args) {

System.out.println("from main function.....");

// InitializerTest i1=new InitializerTest();


// InitializerTest i2=new InitializerTest();
// InitializerTest i3=new InitializerTest();
// InitializerTest i4=new InitializerTest();
}

}
----------------nhập test-----------

---------------------------------------------

You might also like