如何实现包含不同类型对象的Java链接列表结构? [英] How Can I Implement A Java Linked List Structure That Holds Different Types Of Object?

查看:87
本文介绍了如何实现包含不同类型对象的Java链接列表结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个包含不同类型对象的java链表结构,这样我就不会重复每个列表中不同类型的结构和方法





  //  节点类 

public class 节点{

Affectation宾语;
节点suiv;
Node prec;

public 节点(影响对象){

this .object =对象;
}



}

// ListeAffectation类

public class ListeAffectation {

Node tete;
Node queu;


public boolean estvide()
{
return (tete == null);
}


public void insererentete(Affectation) object)
{
Node node = new Node(object);

if (estvide())
{
tete = node;
queu = node;
} else {
node.suiv = tete;
tete.prec = node;
tete = node;
}


}

public void insererenqueu(影响对象)
{
节点node = new Node(object);

if (estvide())
{
tete = node;
queu = node;
} else {

queu.suiv = node;
node.prec = queu;
queu = node;
}


}

public void AfficherListeAffectation()
{
Node p = tete;
int i = 0;
while (p!= null){

System.out.println( element + i + est: );
p.object.Affichage();
System.out.println( \ n ------------ ------------);
p = p.suiv;
i ++;
}
}




}





所以我应该创建另一个具有另一个对象类型的列表并重复相同的方法。或者我必须使用所谓的模板,如果是的话,我该如何使用它们:/



按照我想要的方式我不想使用列表<>或者arraylist<>

解决方案

为什么不使用类Object?

所有其他类都继承自这个类。



或者如果您有特定需求,您可以创建自己的基类,其中包含有关对象的常用信息。例如,名称,大小或类似属性。



你也可以使用泛型(模板),但这需要解释一下,所以我推荐你这个网站:课程:泛型 [ ^ ]


这是<的常见问题i> polymorphism : https://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 [ ^ ]。



经典的OOP模式是这样的:你创建一个抽象类型,它具有你的多态元素集的所有元素共有的所有功能,并使它成为基类对于所有元素类。但是要在集合上执行的所有操作都应该用这个基类的成员来表示。当然,这个基类不应该是 Object (更好地忽略解决方案1的建议);使用这种最顶层类型作为基本类型相当于缺乏使用字符串类型范例。派生类可以具有不同的功能,但应该通过虚拟方法(以及属性)的机制来实现。请参阅: https://en.wikipedia.org/wiki/Dynamic_dispatch [ ^ ]。



换句话说,你必须掌握OOP的整个中心思想:后期绑定和多态,几乎从头开始,并且也了解它的技术机制。另见:

https://en.wikipedia.org/wiki/Late_binding [< a href =https://en.wikipedia.org/wiki/Late_bindingtarget =_ blanktitle =New Window> ^ ],

https://en.wikipedia.org/wiki/Object-oriented_programming [ ^ ]。



-SA

i want to implement a java linked list structure that holds different types of object ,so that i don't repeat the structure and methods in every list with a different type


//node class

public class Node {

	Affectation object;
	Node suiv;
	Node prec;

	public Node(Affectation object) {
		
		this.object=object;
	}
	
	
	
}

//ListeAffectation class

public class ListeAffectation {
	
	Node tete;
	Node queu;
	
	
	public boolean estvide()
		{
		   return (tete==null);	
		}
	
	
	public void  insererentete(Affectation object)
	{
		Node node=new Node(object);
		
		if(estvide())
    	{
			tete=node;
			queu=node;   
     	}else {
			node.suiv=tete;
			tete.prec=node;
			tete=node;
		}
		
		
	}
	
	public void  insererenqueu(Affectation object)
	{
		Node node=new Node(object);
		
		if(estvide())
    	{
			tete=node;
			queu=node;   
     	}else {
			
     	queu.suiv=node;
     	node.prec=queu;
     	queu=node;
     	}
		
		
	}
	
	public void AfficherListeAffectation()
	{
		Node p=tete;
		int i=0;
		while (p!=null) {
			
			System.out.println("element  "+i+" est : ");
			p.object.Affichage();
			System.out.println("\n------------------------");
			p=p.suiv;
			i++;		
		}
	}
	
	
	
	
}



so should i create another list that has another object type and repeat the same methods . or i have to use what called Template if yes so how do i use them :/

by the way i want my own list i dont want to use a list<> or an arraylist<>

解决方案

Why not use the class Object?
All other classes inherit from this class.

Or if you have specific needs you can create your own base class, that holds common information about your objects. For example, Name, Size or similar properties.

You can also use generics (templates), but that is a bit more to explain so I refer you to this website: Lesson: Generics[^]


This is the classic problem of polymorphism: https://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^].

And the classical OOP schema is this: you create an abstract type with all the functionality common to all the element of your polymporphic set of elements and make it the base class for all the element classes. But all the operations to be performed on the set should be expressed in terms of members of this base class. Of course, this base class should not be Object (better disregard the advice of Solution 1); using this topmost type as a base type is equivalent to lack of the use of string typing paradigm. The derived classes can have different functionality, but it should be achieved through the mechanism of virtual methods (and, hence, properties). Please see: https://en.wikipedia.org/wiki/Dynamic_dispatch[^].

In other words, you have to grasp the whole central idea of OOP: late binding and polymorphism, pretty much from scratch, and also understand the technical mechanisms of it. See also:
https://en.wikipedia.org/wiki/Late_binding[^],
https://en.wikipedia.org/wiki/Object-oriented_programming[^].

—SA


这篇关于如何实现包含不同类型对象的Java链接列表结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆