class vs Object vs Instance

class: هو التصنيف, مثال: Human

object:هو نسخة من التصنيف, مثال: h1

instance:هو نسخة حية من التصنيف, يعني يتم اعطائها قيم, مثال: h1.name="adnan"

 

مثال على class وتوابع ضمنه وانشاء object منه وانشاء instance منه:

import java.util.Scanner;

class Human
{
    String name;
    String eyeColor;
    String skinColor;
    int height;
    int weight;
    
    public void walk()
    {
        System.out.println("walking...");
    }
    public void breath()
    {
        System.out.println(name+" is breathing...");
    }
}

class Main {
  public static void main(String[] args) {
  Human obj; //creating object from class Human
  obj=new Human(); //creating instance of class Human
  obj.name="adnan";
  obj.eyeColor="brown";
  obj.breath();
  
  Human obj2; //creating object from class Human
  obj2=new Human(); //creating instance of class Human
  obj2.name="omar";
  obj2.eyeColor="green";
  obj2.breath();
  
}
}

 

نفس المثال السابق بعد اضافة توابع set and get من اجل ال encapsulation التغليف, وايضا مع اضافة توابع constructors, وايضا اضافة تابع tostring

الباني constructor هو حالة خاصة من التوابع, ليس له مخرجات return value, واسمه نفس اسمه اسم التصنيفclass, ويمكن عمل عدد لانهائي من توابع البناء للتصنيف الواحد, والاختلاف بينها يكون بعدد ونوع بيانات المدخلاتparameters

import java.util.Scanner;

class Human
{
    private String name;
    private String eyeColor;
    private String skinColor;
    private int height;
    private int weight;
    private boolean gender;
    
    public Human()
    {
        name="";
        eyeColor="";
        skinColor="";
        height=0;
        weight=0;
        gender=true;
    }
    
    public Human(String name, String eyeColor, String skinColor, int height, int weight, String gender)
    {
        this.name=name;
        this.eyeColor=eyeColor;
        this.skinColor=skinColor;
        this.height=height;
        this.weight=weight;
        setGender(gender);
    }
    
    public void setGender(String gender)
    {
        if(gender=="male")
        this.gender=true;
        else if(gender=="female")
        this.gender=false;
        else System.out.println("error entering gender! it should be either male or female.");
    }
    
    public String getGender()
    {
        if(gender==true)
        return "male";
        else return "female";
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return name;
    }
    
    public void setEyeColor(String eyeColor)
    {
        this.eyeColor=eyeColor;
    }
    
    public String getEyeColor()
    {
        return eyeColor;
    }
    
    public void setSkinColor(String skinColor)
    {
        this.skinColor=skinColor;
    }
    
    public String getSkinColor()
    {
        return skinColor;
    }
    
    public void setHeight(int height)
    {
        this.height=height;
    }
    
    public int getHeight()
    {
        return height;
    }
    
    public void setWeight(int weight)
    {
        this.weight=weight;
    }
    
    public int getWeight()
    {
        return weight;
    }
               
    public void walk()
    {
        System.out.println("walking...");
    }
    public void breath()
    {
        System.out.println(name+" is breathing...");
    }
    public String toString()
    {
        return "name: "+name+", Eye color: "+eyeColor+", Skin color: "+skinColor+", Height: "+height+", Weight: "+weight+", gender: "+getGender();
    }
    
}

class Main {
  public static void main(String[] args) {
  Human obj; //creating object from class Human
  obj=new Human("adnan","brown","white",185,92,"male"); //creating instance of class Human
  obj.breath();
  System.out.println(obj.toString()); //printing object information using the tostring() function
  
  Human obj2; //creating object from class Human
  obj2=new Human(); //creating instance of class Human
  obj2.setName("reem");
  obj2.setGender("female");
  obj2.setEyeColor("green");
  obj2.setSkinColor("white");
  obj2.setHeight(160);
  obj2.setWeight(55);
  obj2.breath();
  System.out.println();
  //printing the object info using get functions:
  System.out.println(obj2.getName());
  System.out.println(obj2.getGender());
  System.out.println(obj2.getEyeColor());
  System.out.println(obj2.getSkinColor());
  System.out.println(obj2.getHeight());
  System.out.println(obj2.getWeight());
}
}

 

 

مثال برنامج بسيط بالجافا java يحوي كلاس واحد للموظف, ويحوي خصائص الموظف و get and set وايضا constructors للكلاس

الباني constructor هو حالة خاصة من التوابع, ليس له مخرجات return value, واسمه نفس اسمه اسم التصنيفclass, ويمكن عمل عدد لانهائي من توابع البناء للتصنيف الواحد, والاختلاف بينها يكون بعدد ونوع بيانات المدخلاتparameters

 

 

package classesExamples;

public class Employee {

 private int id;
 private String name;
 private double basicsalary;
 private double allowances;
 private boolean is_male;
 
 public Employee()
 {
  id=0;
  name="";
  basicsalary=0;
  allowances=0;
  is_male=true;
 }
 public Employee(String b, double c, double d)
 {
  name=b;
  basicsalary=c;
  allowances=d;
 }
 
 public Employee(int a, String b, double c, double d,String gender)
 {
  id=a;
  name=b;
  basicsalary=c;
  allowances=d;
  
  if(gender=="male")
   is_male=true;
  else is_male=false;
 }
 
 public int getId()
 {
  return id;
 }

 public String getName()
 {
  return name;
 }
 public double getSalary()
 {
  return basicsalary;
 }
 public double getAllowances()
 {
  return allowances;
 }

 public String getGender()
 {
  if(is_male==true)
  return "male";
  else return "female";
 }
 
 public void setId(int i)
 {
  id=i;
 }

 public void setname(String x)
 {
  name=x;
 }
 public void setbasicSalary(double v)
 {
  basicsalary=v;
 }
 public void setAllowances(double f)
 {
  allowances=f;
 }

public class EmployeeManagement {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  Employee emp=new Employee();
  
  int empid = emp.getId();
  System.out.println(empid);
  
Employee emp2=new Employee(1,"adnan",1000,120,"male");
  
System.out.println("employee info is: \n----------------------------");

System.out.println("id:"+emp2.getId());
System.out.println("name:"+emp2.getName());
System.out.println("basic salary :"+emp2.getSalary());
System.out.println("allowances:"+emp2.getAllowances());
System.out.println("total salary:"+(emp2.getSalary()+emp2.getAllowances()));
System.out.println("gender :"+emp2.getGender());
  
  
 }


}

 

مثال على الوراثة في جافا:

import java.util.Scanner;

class Creature{
    private String eyeColor;
    private String skinColor;
    private int height;
    private int weight;
    private boolean gender;
    
    public Creature()
    {
        eyeColor="";
        skinColor="";
        height=0;
        weight=0;
        gender=true;
    }
    
    public Creature(String eyeColor, String skinColor, int height, int weight, String gender)
    {
        this.eyeColor=eyeColor;
        this.skinColor=skinColor;
        this.height=height;
        this.weight=weight;
        setGender(gender);
    }
    
    public void setGender(String gender)
    {
        if(gender=="male")
        this.gender=true;
        else if(gender=="female")
        this.gender=false;
        else System.out.println("error entering gender! it should be either male or female.");
    }
    
    public String getGender()
    {
        if(gender==true)
        return "male";
        else return "female";
    }
    
    public void setEyeColor(String eyeColor)
    {
        this.eyeColor=eyeColor;
    }
    
    public String getEyeColor()
    {
        return eyeColor;
    }
    
    public void setSkinColor(String skinColor)
    {
        this.skinColor=skinColor;
    }
    
    public String getSkinColor()
    {
        return skinColor;
    }
    
    public void setHeight(int height)
    {
        this.height=height;
    }
    
    public int getHeight()
    {
        return height;
    }
    
    public void setWeight(int weight)
    {
        this.weight=weight;
    }
    
    public int getWeight()
    {
        return weight;
    }
               
    public void walk()
    {
        System.out.println("walking...");
    }
    public void breath()
    {
        System.out.println(" is breathing...");
    }
    public String toString()
    {
        return " Eye color: "+eyeColor+", Skin color: "+skinColor+", Height: "+height+", Weight: "+weight+", gender: "+getGender();
    }    
}


class Human extends Creature
{
    private String name;

    public Human()
    {
        super("","",0,0,"male");
        name="";
    }
    
    public Human(String name, String eyeColor, String skinColor, int height, int weight, String gender)
    {
        super(eyeColor,skinColor,height,weight,gender);
        this.name=name;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return name;
    }
    /*public void walk()
    {
        System.out.println("walking...");
    }
    public void breath()
    {
        System.out.println(name+" is breathing...");
    }*/
    /*public String toString()
    {
        return "name: "+name+", Eye color: "+eyeColor+", Skin color: "+skinColor+", Height: "+height+", Weight: "+weight+", gender: "+getGender();
    }*/
}

class Animal extends Creature
{
    private int price;
    
    public Animal()
    {
        super("","",0,0,"male");
        price=0;
    }
    
    public Animal(int price,String eyeColor, String skinColor, int height, int weight, String gender)
    {
        super(eyeColor,skinColor,height,weight,gender);
        this.price=price;
    }

    public void setPrice(int price)
    {
        this.price=price;
    }
    
    public int getPrice()
    {
        return price;
    }
               
    /*public void walk()
    {
        System.out.println("walking...");
    }
    public void breath()
    {
        System.out.println(name+" is breathing...");
    }
    public String toString()
    {
        return "name: "+name+", Eye color: "+eyeColor+", Skin color: "+skinColor+", Height: "+height+", Weight: "+weight+", gender: "+getGender();
    }*/
}

class Main {
  public static void main(String[] args) {
  Human obj; //creating object from class Human
  obj=new Human("adnan","brown","white",185,92,"male"); //creating instance of class Human
  obj.breath();
  System.out.println(obj.toString()); //printing object information using the tostring() function
  
  Human obj2; //creating object from class Human
  obj2=new Human(); //creating instance of class Human
  obj2.setName("reem");
  obj2.setGender("female");
  obj2.setEyeColor("green");
  obj2.setSkinColor("white");
  obj2.setHeight(160);
  obj2.setWeight(55);
  obj2.breath();
  System.out.println();
  //printing the object info using get functions:
  System.out.println(obj2.getName());
  System.out.println(obj2.getGender());
  System.out.println(obj2.getEyeColor());
  System.out.println(obj2.getSkinColor());
  System.out.println(obj2.getHeight());
  System.out.println(obj2.getWeight());
}
}

 

 

ابحث عن مسائل برمجة جافا | Java programming بالانجليزي

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول